An efficient in-place sorting algorithm. [Time: O(nlgn), Space: O(1)]
Idea
The idea of heap sort consists of two phases. In the first phase, the array is transformed into a binary max heap, a tree where each node of the tree is larger than or equal to its children (if any).
The root of a max heap is always the maximum. In the second phase, the array gradually becomes sorted by moving the root to the sorted region and fixing the heap with an algorithm named sift-down. Both phases have a time complexity of O(nlgn).
Implementation
Below is an implementation of heap sort in golang.
// HeapSort Best: O(nlgn), Average: O(nlgn), Worst: O(nlgn), Space: O(1) funcHeapSort(arr []int) { // heapify the array into a max-heap heapify(arr) for i := len(arr) - 1; i > 0; i-- { // swap the root of the max-heap with the last item arr[0], arr[i] = arr[i], arr[0] // fix heap siftDown(arr, 0, i) } }
funcheapify(arr []int) { for i := (len(arr) - 1) / 2; i >= 0; i-- { siftDown(arr, i, len(arr)) } }
funcsiftDown(heap []int, lo, hi int) { root := lo for { child := root*2 + 1 if child >= hi { break } if child+1 < hi && heap[child] < heap[child+1] { child++ } if heap[root] < heap[child] { heap[root], heap[child] = heap[child], heap[root] root = child } else { break }