Heap Sort
Code
C language code:
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
void heapify(int arr[], int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[left] > arr[largest])
largest = left;
if (right < n && arr[right] > arr[largest])
largest = right;
if (largest != i) {
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
}
}
void heap_sort(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);
}
}
int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Given array is \n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
heap_sort(arr, n);
printf("\nSorted array is \n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
Code Explanation
- Initialization: The array
arris the array to be sorted, andnis the length of the array. - Swap function:
- The
swapfunction is used to swap two integer values.
- The
- Heapify function:
- The
heapifyfunction is used to adjust a subtree into a max-heap. largestis initially set to the root nodei.leftis the left child, andrightis the right child.- Compares the root, left child, and right child to find the maximum value.
- If the maximum is not the root, swaps and recursively heapifies the affected subtree.
- The
- Heap sort function:
heap_sortfirst builds a max-heap.- Starting from the last non-leaf node, heapifies each node upward.
- Then one by one moves the maximum element (root) to the end of the array and re-heapifies the remaining elements.
Example Run
Assuming the input array is {12, 11, 13, 5, 6, 7}:
- Initial state:
{12, 11, 13, 5, 6, 7} - Build max-heap:
- Heapify from the last non-leaf node (index 2):
{12, 11, 13, 5, 6, 7}->{12, 11, 13, 5, 6, 7} - Heapify index 1:
{12, 11, 13, 5, 6, 7}->{12, 13, 11, 5, 6, 7} - Heapify index 0:
{12, 13, 11, 5, 6, 7}->{13, 12, 11, 5, 6, 7}
- Heapify from the last non-leaf node (index 2):
- Start sorting:
- Swap root with last element:
{13, 12, 11, 5, 6, 7}->{7, 12, 11, 5, 6, 13} - Heapify root:
{7, 12, 11, 5, 6, 13}->{12, 7, 11, 5, 6, 13} - Swap root with second-to-last element:
{12, 7, 11, 5, 6, 13}->{6, 7, 11, 5, 12, 13} - Heapify root:
{6, 7, 11, 5, 12, 13}->{11, 7, 6, 5, 12, 13} - Continue the above process until the array is fully sorted.
- Swap root with last element:
- Final result:
{5, 6, 7, 11, 12, 13}
Time Complexity Analysis
The time complexity of heap sort mainly depends on the heap-building and sorting processes.
- Best-case time complexity: , because each heapify operation takes logarithmic time.
- Worst-case time complexity: , regardless of the initial order of the input array, the number of steps executed is essentially the same.
- Average time complexity: , for most input arrays, heap sort's performance is very stable.
Space Complexity Analysis
Heap sort is an in-place sorting algorithm that requires no additional auxiliary space, so the space complexity is .
Advantages and Disadvantages
Advantages:
- Stable time complexity; suitable for large-scale data sorting.
- No additional auxiliary space needed; low space complexity.
- Time complexity remains consistent regardless of the initial order of the input array.
Disadvantages:
- Unstable sorting algorithm; may change the relative order of equal elements.
- Relatively high implementation complexity; more complex to understand and code than simple sorting algorithms.
Applicable Scenarios
Heap sort is suitable for the following scenarios:
- Sorting large datasets.
- When a highly efficient sorting algorithm with low space complexity is needed.
- Scenarios where stable sorting is not required.
Summary
Heap sort is an efficient sorting algorithm that works by building a max-heap, moving the maximum element one by one to the end of the array, and re-heapifying the remaining elements. Although its implementation complexity is relatively high and it is unstable, it performs excellently when handling large-scale data and is a sorting algorithm with stable time complexity and low space complexity.