Heap Sort
Heap sort processes the elements by creating the min heap or max heap using the elements of the given array. Min heap or max heap represents the ordering of the array in which root element represents the minimum or maximum element of the array. At each step, the root element of the heap gets deleted and stored into the sorted array and the heap will again be heapified.
The heap sort basically recursively performs two main operations.
- Build a heap H, using the elements of ARR.
- Repeatedly delete the root element of the heap formed in phase 1.
Complexity
Complexity | Best Case | Average Case | Worst case |
---|---|---|---|
Time Complexity | Ω(n log (n)) | θ(n log (n)) | O(n log (n)) |
Space Complexity | O(1) |
Algorithm
HEAP_SORT(ARR, N)
- Step 1: [Build Heap H]
Repeat for i=0 to N-1
CALL INSERT_HEAP(ARR, N, ARR[i])
[END OF LOOP] - Step 2: Repeatedly Delete the root element
Repeat while N > 0
CALL Delete_Heap(ARR,N,VAL)
SET N = N+1
[END OF LOOP] - Step 3: END
C Program
- #include<stdio.h>
- int temp;
- void heapify(int arr[], int size, int i)
- {
- int largest = i;
- int left = 2*i + 1;
- int right = 2*i + 2;
- if (left < size && arr[left] >arr[largest])
- largest = left;
- if (right < size && arr[right] > arr[largest])
- largest = right;
- if (largest != i)
- {
- temp = arr[i];
- arr[i]= arr[largest];
- arr[largest] = temp;
- heapify(arr, size, largest);
- }
- }
- void heapSort(int arr[], int size)
- {
- int i;
- for (i = size / 2 - 1; i >= 0; i--)
- heapify(arr, size, i);
- for (i=size-1; i>=0; i--)
- {
- temp = arr[0];
- arr[0]= arr[i];
- arr[i] = temp;
- heapify(arr, i, 0);
- }
- }
- void main()
- {
- int arr[] = {1, 10, 2, 3, 4, 1, 2, 100,23, 2};
- int i;
- int size = sizeof(arr)/sizeof(arr[0]);
- heapSort(arr, size);
- printf("printing sorted elements\n");
- for (i=0; i<size; ++i)
- printf("%d\n",arr[i]);
- }
Output:
printing sorted elements 1 1 2 2 2 3 4 10 23 100
Java Program
- #include<stdio.h>
- int temp;
- void heapify(int arr[], int size, int i)
- {
- int largest = i;
- int left = 2*i + 1;
- int right = 2*i + 2;
- if (left < size && arr[left] >arr[largest])
- largest = left;
- if (right < size && arr[right] > arr[largest])
- largest = right;
- if (largest != i)
- {
- temp = arr[i];
- arr[i]= arr[largest];
- arr[largest] = temp;
- heapify(arr, size, largest);
- }
- }
- void heapSort(int arr[], int size)
- {
- int i;
- for (i = size / 2 - 1; i >= 0; i--)
- heapify(arr, size, i);
- for (i=size-1; i>=0; i--)
- {
- temp = arr[0];
- arr[0]= arr[i];
- arr[i] = temp;
- heapify(arr, i, 0);
- }
- }
- void main()
- {
- int arr[] = {1, 10, 2, 3, 4, 1, 2, 100, 23, 2};
- int i;
- int size = sizeof(arr)/sizeof(arr[0]);
- heapSort(arr, size);
- printf("printing sorted elements\n");
- for (i=0; i<size; ++i)
- printf("%d\n",arr[i]);
- }
Output:
printing sorted elements 1 1 2 2 2 3 4 10 23 100
C# program
- using System;
- public class HeapSorting {
- static void heapify(int[] arr, int size, int i)
- {
- int largest = i;
- int left = 2*i + 1;
- int right = 2*i + 2;
- int temp;
- if (left < size && arr[left] > arr[largest])
- largest = left;
- if (right < size && arr[right] > arr[largest])
- largest = right;
- if (largest != i)
- {
- temp = arr[i];
- arr[i]= arr[largest];
- arr[largest] = temp;
- heapify(arr, size, largest);
- }
- }
- static void heapSort(int[] arr, int size)
- {
- int i;
- int temp;
- for (i = size / 2 - 1; i >= 0; i--)
- heapify(arr, size, i);
- for (i=size-1; i>=0; i--)
- {
- temp = arr[0];
- arr[0]= arr[i];
- arr[i] = temp;
- heapify(arr, i, 0);
- }
- }
- public void Main()
- {
- int[] arr = {1, 10, 2, 3, 4, 1, 2, 100, 23, 2};
- int i;
- heapSort(arr, 10);
- Console.WriteLine("printing sorted elements");
- for (i=0; i<10; ++i)
- Console.WriteLine(arr[i]);
- }
- }
Output:
printing sorted elements 1 1 2 2 2 3 4 10 23 100