(Solved) : Working C Heapsort Assignment Need Min Order Heap Sort Giving Right Answer Include Using N Q42762916 . . .

I am working on my C++ Heapsortassignment. We need to do a MIN-ORDERheap sort. This is what I have, but its not giving me the rightanswer.

#include <iostream>

using namespace std;

// To heapify a subtree rooted with node i which is
// an index in arr[]. N is size of heap
void heapify(int arr[], int n, int i)
{
   int smallest = i; // Initialize smallest as root
   int l = 2 * i + 1; // left = 2*i + 1
   int r = 2 * i + 2; // right = 2*i + 2

   // If left child is smaller than root
   if (l < n && arr[l] <arr[smallest])
       smallest = l;

   // If right child is smaller than smallest sofar
   if (r < n && arr[r] <arr[smallest])
       smallest = r;

   // If smallest is not root
   if (smallest != i)
   {
       swap(arr[i], arr[smallest]);

       // Recursively heapify theaffected sub-tree
       heapify(arr, n, smallest);
   }
}

// Function to delete the root from Heap
void deleteRoot(int arr[], int& n)
{
   // Get the last element
   int lastElement = arr[n – 1];

   // Replace root with last element
   arr[0] = lastElement;

   // Decrease size of heap by 1
   n = n – 1;

   // heapify the root node
   heapify(arr, n, 0);
}

/* 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”;
}

// Driver Code
int main()
{
   int arr[] = { 10, 9, 6, 4, 5, 8, 7};

   int n = sizeof(arr) / sizeof(arr[0]);

   printArray(arr, n);

   deleteRoot(arr, n);

   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…

Leave a Comment

About

We are the best freelance writing portal. Looking for online writing, editing or proofreading jobs? We have plenty of writing assignments to handle.

Quick Links

Browse Solutions

Place Order

About Us

× How can I help you?