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...
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...
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; }
I have an error but i can't correct it #include <iostream> using namespace       std; long reverse...
I have an error but i can't correct it #include <iostream> using namespace       std; long reverse (long       num, long   equation,long reverse = 0); int       main() {               long       num, reverse = 0;        cout << "Enter       the       num:       ";        cin >> num;        cout << "Reverse       num       is       =       "               << reverse << endl;        return       0; } long reverse(long       num, long equation, long reverse = 0) {        while (num)        {               equation...
C ++ #include <fstream> #include <iostream> using namespace std; int main() { float bmi; ifstream inFile;...
C ++ #include <fstream> #include <iostream> using namespace std; int main() { float bmi; ifstream inFile; inFile.open("bmi.txt"); while (!inFile.eof()) { inFile >> bmi; if( bmi < 18.5) { cout << bmi << " is underweight " ; } else if( bmi >= 18.5 && bmi <= 24.9) { cout << bmi << " is in normal range " ; } else if( bmi >= 25.0 && bmi <= 29.9) { cout << bmi << " is overweight " ; }...
Consider the following program: #include #include #include using namespace std; int main() { int num1; int...
Consider the following program: #include #include #include using namespace std; int main() { int num1; int num2; cout << fixed << showpoint << setprecision(2); cout << "Enter two integers: "; cin >> num1 >> num2; cout << endl; if (num1 != 0 && num2 != 0) cout << sqrt(abs(num1 + num2) + 0.5) << endl; else if (num1 != 0) cout << floor(num1 + 0.5) << endl; else if (num2 != 0) cout << ceil(num2 + 0.5) << endl; else...
Need someone to fix my code: #include<iostream> using namespace std; struct student { double firstQuizz; double...
Need someone to fix my code: #include<iostream> using namespace std; struct student { double firstQuizz; double secondQuizz; double midTerm; double finalTerm; string name; }; int main() { int n; cout<<"enter the number of students"<<endl; cin>>n; struct student students[n]; int i; struct student istudent; for(i=0;i<n;i++) {    cout<<"Student name?"; cin >> istudent.name; cout<<"enter marks in first quizz , second quizz , mid term , final term of student "<<i+1<<endl; cin>>students[i].firstQuizz>>students[i].secondQuizz>>students[i].midTerm>>students[i].finalTerm; } for(i=0;i<n;i++) { double marks=0; double score=students[i].firstQuizz+students[i].secondQuizz+students[i].midTerm+students[i].finalTerm; marks=(students[i].firstQuizz*0.25)+(students[i].secondQuizz*0.25)+(students[i].midTerm*0.25)+(students[i].finalTerm*0.50); double totalArrgegateMarks =...
#include<iostream> using namespase std; int main() {     int i=1;     int j=-5;     int k=80;...
#include<iostream> using namespase std; int main() {     int i=1;     int j=-5;     int k=80;     float f=2.22;     float h=-7.5;     char a='S';     char b='M';     cout<<i<<"\t"<<j<<"\t"<<k<<endl;     Cout<<f<<"\t"<<h<<endl;     cout<<a<<"\t"<<b<<endl;     return 0;     }