Introduction
Heap Sort is an algorithm that leverages the properties of a binary heap data structure. It is efficient and reliable in handling large datasets. This article explores the working mechanism, implementation, and complexity analysis of the Heap Sort.
Heap Sort in a Nutshell
Heap Sort transforms the input array into a binary heap, a binary tree (a complete binary tree to be precise) that satisfies the heap property. A binary heap can be of the following types:
- Max Heap: The parent node is always equal to or greater than its child nodes.
- Min Heap: The parent node is always equal to or less than its child nodes.
Heap Sort typically utilizes a max heap to sort elements in ascending order.
How Heap Sort Works
Heap Sort consists of two main phases:
- Building a Max Heap:
- Convert the unsorted array into a max heap by ensuring the heap property is satisfied for each node.
- Sorting the Array:
- Repeatedly swap the root (largest element) with the last element of the heap and reduce the heap size.
- Restore the heap property by heapifying the root node.
- Repeat until all elements are sorted.
Heap Sort Algorithm
Below is the step-by-step algorithm for Heap Sort:
- Build a Max Heap
- Start from the last non-leaf node and move upward.
- Apply heapify operation to maintain the max heap property.
- Extract Elements from the Heap
- Swap the root (maximum value) with the last element.
- Reduce the heap size.
- Apply heapify to restore the heap structure.
- Repeat until all elements are sorted.
Heap Sort Implementation in Java
The following is a Java implementation of Heap Sort:
@Test
public void HeapSortTest() throws Exception {
int[] toSort = {13, 15, 21, 57, 67, 71};
sortArray(toSort);
for (int elem : toSort) {
System.out.println(elem + " ");
}
}
private void sortArray(int[] toSort) {
int len = toSort.length;
for (int i = len/2 - 1; i >= 0; i--)
heapifyArray(toSort, len, i);
for (int i = len - 1; i >= 0; i--) {
int temp = toSort[0];
toSort[0] = toSort[i];
toSort[i] = temp;
heapifyArray(toSort, i, 0);
}
}
void heapifyArray(int[] toSort, int n, int i) {
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l < n && toSort[l] > toSort[largest])
largest = l;
if (r < n && toSort[r] > toSort[largest])
largest = r;
if (largest != i) {
int swap = toSort[i];
toSort[i] = toSort[largest];
toSort[largest] = swap;
heapifyArray(toSort, n, largest);
}
}
Time and Space Complexity of Heap Sort
- Best Case: O(n log n)
- Average Case: O(n log n)
- Worst Case: O(n log n)
- Space Complexity: O(1) (Heap Sort is an in-place sorting algorithm)
Advantages of Heap Sort
- Efficient for large datasets.
- Performs well with minimal auxiliary space.
- Guarantees O(n log n) time complexity in all cases.
Disadvantages of Heap Sort
- Not as fast as Quick Sort in practical applications.
- Heapify operations involve multiple comparisons and swaps, leading to overhead.
Conclusion
Heap Sort is a sorting algorithm for applications requiring stability and efficiency in sorting large datasets. It is not as fast as Quick Sort in practice, but its worst-case time complexity is O(n log n). Understanding Heap Sort enhances one’s grasp of algorithmic problem-solving and data structures in computing.
You can find the example code on Github.
