Iterative Heap Sort

Heap Sort is a comparison based sorting technique where we first build Max Heap and then swaps the root element with last element (size times) and maintains the heap property each time to finally make it sorted.

Examples:

Input :  10 20 15 17 9 21
Output : 9 10 15 17 20 21 

Input:  12 11 13 5 6 7 15 5 19
Output: 5 5 6 7 11 12 13 15 19 

In first Example, first we have to build Max Heap.
So, we will start from 20 as child and check for its parent. Here 10 is smaller, so we will swap these two.
Now, 20 10 15 17 9 21
Now, child 17 is greater than its parent 10. So, both will be swapped and order will be
20 17 15 10 9 21
Now, child 21 is greater than parent 15. So, both will be swapped.
20 17 21 10 9 15
Now, again 21 is bigger than parent 20. So,
21 17 20 10 9 15
This is Max Heap.

Now, we have to apply sorting. Here, we have to swap first element with last one and we have to maintain Max Heap property.
So, after first swapping : 15 17 20 10 9 21
It clearly violates Max Heap property. So, we have to maintain it. So, order will be
20 17 15 10 9 21
17 10 15 9 20 21
15 10 9 17 20 21
10 9 15 17 20 21
9 10 15 17 20 21
Here, underlined part is sorted part.



// C++ program for implementation 
// of Iterative Heap Sort
#include <bits/stdc++.h>
using namespace std;

// function build Max Heap where value 
// of each child is always smaller
// than value of their parent
void buildMaxHeap(int arr[], int n) 
{ 
    for (int i = 1; i < n; i++) 
    {
        // if child is bigger than parent
        if (arr[i] > arr[(i - 1) / 2]) 
        {
            int j = i;
    
            // swap child and parent until
            // parent is smaller
            while (arr[j] > arr[(j - 1) / 2]) 
            {
                swap(arr[j], arr[(j - 1) / 2]);
                j = (j - 1) / 2;
            }
        }
    }
}

void heapSort(int arr[], int n) 
{
    buildMaxHeap(arr, n);

    for (int i = n - 1; i > 0; i--)
    {
        // swap value of first indexed 
        // with last indexed 
        swap(arr[0], arr[i]);
    
        // maintaining heap property
        // after each swapping
        int j = 0, index;
        
        do
        {
            index = (2 * j + 1);
            
            // if left child is smaller than 
            // right child point index variable 
            // to right child
            if (arr[index] < arr[index + 1] &&
                                index < (i - 1))
                index++;
        
            // if parent is smaller than child 
            // then swapping parent with child 
            // having higher value
            if (arr[j] < arr[index] && index < i)
                swap(arr[j], arr[index]);
        
            j = index;
        
        } while (index < i);
    }
}

// Driver Code to test above
int main() 
{
    int arr[] = {10, 20, 15, 17, 9, 21};
    int n = sizeof(arr) / sizeof(arr[0]);
    
    printf("Given array: ");
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);
        
    printf("\n\n"); 

    heapSort(arr, n);

    // print array after sorting
    printf("Sorted array: ");
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);

    return 0;
}

Output :

Given array: 10 20 15 17 9 21 

Sorted array: 9 10 15 17 20 21 

Here, both function buildMaxHeap and heapSort runs in O(nlogn) time. So, overall time complexity is O(nlogn)





Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



3 Average Difficulty : 3/5.0
Based on 1 vote(s)