tree/heap-sort
Heap Sort
- best
- O(n log n)
- average
- O(n log n)
- worst
- O(n log n)
- space
- O(1)
- stability
- unstable
- method
- comparison
pick a language to open the code
def sift_down(a, root, end): while True: child = 2 * root + 1 if child >= end: return if child + 1 < end and a[child] < a[child + 1]: child += 1 if a[root] >= a[child]: return a[root], a[child] = a[child], a[root] root = child def heap_sort(a): n = len(a) for root in range(n // 2 - 1, -1, -1): sift_down(a, root, n) for end in range(n - 1, 0, -1): a[0], a[end] = a[end], a[0] sift_down(a, 0, end)
function siftDown(a, root, end) { for (;;) { let child = 2 * root + 1; if (child >= end) return; if (child + 1 < end && a[child] < a[child + 1]) child++; if (a[root] >= a[child]) return; [a[root], a[child]] = [a[child], a[root]]; root = child; } } function heapSort(a) { for (let root = (a.length >> 1) - 1; root >= 0; root--) { siftDown(a, root, a.length); } for (let end = a.length - 1; end > 0; end--) { [a[0], a[end]] = [a[end], a[0]]; siftDown(a, 0, end); } }
static void swap(int a[], int i, int j) { int t = a[i]; a[i] = a[j]; a[j] = t; } static void sift_down(int a[], int root, int end) { for (;;) { int child = 2 * root + 1; if (child >= end) return; if (child + 1 < end && a[child] < a[child + 1]) child++; if (a[root] >= a[child]) return; swap(a, root, child); root = child; } } void heap_sort(int a[], int n) { for (int root = n / 2 - 1; root >= 0; root--) sift_down(a, root, n); for (int end = n - 1; end > 0; end--) { swap(a, 0, end); sift_down(a, 0, end); } }
static void sift_down(std::vector<int>& a, int root, int end) { for (;;) { int child = 2 * root + 1; if (child >= end) return; if (child + 1 < end && a[child] < a[child + 1]) child++; if (a[root] >= a[child]) return; std::swap(a[root], a[child]); root = child; } } void heap_sort(std::vector<int>& a) { int n = static_cast<int>(a.size()); for (int root = n / 2 - 1; root >= 0; root--) sift_down(a, root, n); for (int end = n - 1; end > 0; end--) { std::swap(a[0], a[end]); sift_down(a, 0, end); } }
static void SiftDown(int[] a, int root, int end) { while (true) { int child = 2 * root + 1; if (child >= end) return; if (child + 1 < end && a[child] < a[child + 1]) child++; if (a[root] >= a[child]) return; (a[root], a[child]) = (a[child], a[root]); root = child; } } static void HeapSort(int[] a) { for (int root = a.Length / 2 - 1; root >= 0; root--) { SiftDown(a, root, a.Length); } for (int end = a.Length - 1; end > 0; end--) { (a[0], a[end]) = (a[end], a[0]); SiftDown(a, 0, end); } }
static void swap(int[] a, int i, int j) { int t = a[i]; a[i] = a[j]; a[j] = t; } static void siftDown(int[] a, int root, int end) { while (true) { int child = 2 * root + 1; if (child >= end) return; if (child + 1 < end && a[child] < a[child + 1]) child++; if (a[root] >= a[child]) return; swap(a, root, child); root = child; } } static void heapSort(int[] a) { for (int root = a.length / 2 - 1; root >= 0; root--) { siftDown(a, root, a.length); } for (int end = a.length - 1; end > 0; end--) { swap(a, 0, end); siftDown(a, 0, end); } }