Answer to Question #204728 in Algorithms for shaves

Question #204728

HeapSort an array with constant space complexity O(1).


1
Expert's answer
2021-06-09T04:25:34-0400

As we know that, the heap is built inside the array to be sorted so O(1) additional space is required.

void heapSort(int arr[], int n)
{
     for (int i = n / 2 - 1; i >= 0; i--)
       heapify(arr, n, i);
    for (int i = n - 1; i > 0; i--) {
        swap(arr[0], arr[i]);
        heapify(arr, i, 0);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS