Question

in C++ Need a heap-sort function #include <iostream> #include <stdlib.h> #include <string> using namespace std; void...

in C++ Need a heap-sort function

#include <iostream>

#include <stdlib.h>

#include <string>

using namespace std;

void MyFunc ( int *array ) {

// Your code here -----------------

}

int main(int argc,char **argv) {

int *Sequence;

int arraySize;

// Get the size of the sequence

cin >> arraySize;

// Allocate enough memory to store "arraySize" integers

Sequence = new int[arraySize];

  

// Read in the sequence

for ( int i=0; i<arraySize; i++ )

cin >> Sequence[i];

// Run your algorithms to manipulate the elements in Sequence

MyFunc(Sequence);

  

// Output the result

for(int i=0; i<arraySize; i++)

cout << Sequence[i] << endl;

  

// Free allocated space

delete[] Sequence;

}

Homework Answers

Answer #1

main.cpp:

#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

// swap two number
void swap(int &f, int &s){
   int temp = f;
   f = s;
   s = temp;
}

// function to generate max heap
void MaxHeapify(int *array, int s, int len)
{
int lChild = 2 * s + 1; // left child = 2*i + 1
int rChild = 2 * s + 2; // right child = 2*i + 2
int l = s;
  
if (lChild < len && array[lChild] > array[l]) // compare root with left child
l = lChild;
  
if (rChild < len && array[rChild] > array[l]) // compare root with right child
l = rChild;
  
if (l != s) { // if largest is not root
swap(array[s], array[l]); // swap root with largest child
MaxHeapify(array, len, l); // call MaxHeapify recursively
}
}

// heapsort function
void MyFunc ( int *array, int len) {
   // Your code here -----------------
   // generate max heap
for (int i = len / 2 - 1; i >= 0; i--)
MaxHeapify(array, i, len);
   len--;
while (len >= 0) {
swap(array[0], array[len]); // swap first element with last element
MaxHeapify(array, 0, len); // generate max heap
len--;
}
}

int main(int argc,char **argv) {
   int *Sequence;
   int arraySize;
   // Get the size of the sequence
   cin >> arraySize;
   // Allocate enough memory to store "arraySize" integers
   Sequence = new int[arraySize];
  
   // Read in the sequence
   for ( int i=0; i<arraySize; i++ )
       cin >> Sequence[i];
  
   // Run your algorithms to manipulate the elements in Sequence
   MyFunc(Sequence, arraySize);
  
   // Output the result
   for(int i=0; i<arraySize; i++)
       cout << Sequence[i] << endl;
  
   // Free allocated space
   delete[] Sequence;

}

Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Quick sort func in C++ #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc...
Quick sort func in C++ #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc ( int *array ) { // Code here } int main(int argc,char **argv) { int *Sequence; int arraySize; // Get the size of the sequence cin >> arraySize; // Allocate enough memory to store "arraySize" integers Sequence = new int[arraySize];    // Read in the sequence for ( int i=0; i<arraySize; i++ ) cin >> Sequence[i]; // Run your algorithms to manipulate the elements...
Analyze the following program and write down the output. # include <iostream> using namespace std;    void...
Analyze the following program and write down the output. # include <iostream> using namespace std;    void modifyArray( int [ ], int );    void modifyElement ( int );      int main( ) {   const int arraySize = 8;   int a[arraySize] = { 2, -2, 10, -3, -1 ,0, 10, -5 };      modifyArray ( a, arraySize);      for ( int i =0; i < arraySize; i++)               cout << a[i] << ‘  ’;         modifyElement ( a[4] );          for ( int i =0; i <...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in airplane const int COLS = 4; void menu(); //displays options void displaySeats(char[][COLS]); void reserveSeat(char [ROWS][COLS]); int main() { int number=0; //holder variable char seatChar[ROWS][COLS]; for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { seatChar[i][j] = '-'; } } int choice; //input from menu bool repeat = true; //needed for switch loop while (repeat...
//Calculate the even parity for letters, (A, Z, D, a). #include <iostream> #include <bitset> using namespace...
//Calculate the even parity for letters, (A, Z, D, a). #include <iostream> #include <bitset> using namespace std; int main() { char c; int bitCounts, pBit;; while (cin>>c){ //ctrl-Z to stop bitset<8> bs(c); cout<<bs<<endl; bitCounts = bs.count(); pBit = bitCounts % 2? 1: 0; cout<<"Even Parity bit: "<<pBit<<endl; } }
What will be the output of the following code? #include <iostream> using namespace std; int main()...
What will be the output of the following code? #include <iostream> using namespace std; int main() {    char *s = "hello";    char *p = s;    cout << *(p+3) << " " << s[1] << endl; }
This code it's not working, fix it for me please #include <iostream> using namespace std; class...
This code it's not working, fix it for me please #include <iostream> using namespace std; class People {    string name;    double height; public:    void setName(string name)    {        this->name = name;    }    void setHeight(double height)    {        this->height = height;    }    double getHeight() {        return height;    }    string getName()    {        return name;    } }; int main() {    const int size...
Rewrite the following program using switch statements. //Set 7.1 #include <iostream> #include <iomanip> using namespace std;...
Rewrite the following program using switch statements. //Set 7.1 #include <iostream> #include <iomanip> using namespace std; int main() {        int x;        cout << "Selection option " << endl;        cin >> x;        if (x == 1)               cout << "You select option 1" << endl;        else if (x >= 2 || x <= 4)               cout << "You select options 2 or 3 or 4" << endl;               else if (x == 10)               cout << "You select option 10" << endl;               else...
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath>...
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath> using namespace std; void divisors(int num); int main () {    char repeat;    int num;       while (repeat !='n')    {        cout << "Enter a number: ";        cin >> num;        divisors(num);        cout << "Continue? (y or n): ";        cin >> repeat;    }    return 0; } void divisors(int num) {   ...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std;...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std; int theArray[] = { 5, 11, -29, 45, 9, -1}; void sumPos(int ary[], int len, int &sum) {    sum = 0;    for (int i = 0; i < len; i++)            if (ary[i] > 0)                sum = sum + ary[i]; } int main() {    int total;    sumPos(theArray, 6, total);    for (int k=0; k < 6; k++)      cout...
How to trace a c++ program by hand #include<iostream> using namespace std;    class Test {...
How to trace a c++ program by hand #include<iostream> using namespace std;    class Test {     int value; public:     Test(int v); };    Test::Test(int v) {     value = v; }    int main() {     Test t[100];     return 0; } _______________ #include <iostream> using namespace std; int main() { int i,j; for (i=1; i<=3; i++) { for(j=1; j<=i; j++ ) { cout<<"*"; } cout << "\n";   } return 0; }
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • Choose a suitable language/technology/(ies) and implement connection to a database. Once connected to the database, your...
    asked 2 minutes ago
  • POWERSHELL: Create a hash table with the following fields containing appropriate values: (1) Product name Price...
    asked 10 minutes ago
  • IN PYTHON Add a while loop to this code so that it will replay the Game...
    asked 20 minutes ago
  • Build the subnetting tables for the following data (Classful addressing): IP: 192.44.82.16 Mask:255.255.255.192
    asked 28 minutes ago
  • If a future stream of payments is delayed, then the present value of that future stream...
    asked 34 minutes ago
  • Assume the random variable X is normally distributed with mean u=55 and standard deviation sigmaσequals=7. Compute...
    asked 34 minutes ago
  • John knows that monthly demand for his product follows a normal distribution with a mean of...
    asked 51 minutes ago
  • No one theory can account for all crimes. which theory of crime causation do you think...
    asked 1 hour ago
  • Based on the below data: Customer1:Bread,Cereals,Milk Customer2:Tomatoes,Eggs Customer3:Pork,Bread,Milk Customer4:Sugar,Tomatoes,Pork,Bread Customer5:Vinegar Customer6:Eggs,Milk,Cereals,Sugar,Pork Customer7:Eggs,Milk,Vinegar Customer8:Sugar,Pork explain h
    asked 1 hour ago
  • Pleas write a program that checks if any array of string is a plaindrome required input:...
    asked 1 hour ago
  • A parallel-plate capacitor with plates of area 720 cm2 is charged to a potential difference V...
    asked 1 hour ago
  • Develop an algorithm and a program in C that asks the user to enter a positive...
    asked 1 hour ago