I am working on my C++ Heapsort assignment. We need to do aMIN-ORDER heap sort. This is what I have,but its not giving me the right answer. Again INEED to INSERT a value.
// C++ program to insert new element toHeap
#include <iostream>
using namespace std;
#define MAX 1000
// Function to heapify ith node in a Heap
// of size n following a Bottom-up approach
void heapify(int arr[], int n, int i)
{
// Find parent
int parent = (i – 1) / 2;
if (arr[parent] > 0)
{
if (arr[i] < arr[parent]){
swap(arr[i],arr[parent]);
//Recursively heapify the parent node
heapify(arr, n,parent);
}
}
}
// Function to insert a new node to the Heap
void insertNode(int arr[], int& n, int Key)
{
// Increase the size of Heap by 1
n = n + 1;
// Insert the element at end of Heap
arr[n – 1] = Key;
// Heapify the new node following a
// Bottom-up approach
heapify(arr, n, n – 1);
}
// A utility function to print array of size n
void printArray(int arr[], int n)
{
for (int i = 0; i < n; ++i)
cout << arr[i] << “”;
cout << “n”;
}
int main()
{
int arr[MAX] = { 10, 5, 3, 2};
int n = 4;
int key = 1;
printArray(arr, n);
insertNode(arr, n, key);
printArray(arr, n);
return 0;
}
Expert Answer
Answer to I am working on my C++ Heapsort assignment. We need to do a MIN-ORDER heap sort. This is what I have, but its not giving…