-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap_sort_in_place.py
More file actions
62 lines (46 loc) · 1.26 KB
/
heap_sort_in_place.py
File metadata and controls
62 lines (46 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""Implement Heap sort in place.
Sources:
* https://www.geeksforgeeks.org/heap-sort/
* https://www.geeksforgeeks.org/python-program-for-heap-sort/
"""
def _max_child(arr, n, i):
l = 2 * i + 1
if l >= n:
return -1
r = l + 1
if r >= n:
return l
if arr[l] > arr[r]:
return l
return r
def _heapify(arr, n, i):
# Heapify subtree with root at index `i`
# This function assumes that the children
# of the root have already been heapified.
max_child = _max_child(arr, n, i)
if max_child == -1:
return
if arr[i] > arr[max_child]:
return
arr[i], arr[max_child] = arr[max_child], arr[i]
_heapify(arr, n, max_child)
def heap_sort_in_place(arr):
n = len(arr)
# Apply _heapify bottom-up.
# This is slightly different than the Heap implementation,
# because I don't have the [0] prefix at the start
# of the array.
for i in range(n // 2 - 1, -1, -1):
_heapify(arr, n, i)
for i in range(n - 1, 0, -1):
# Take the max value in the heap
# and place it at position `i`.
arr[i], arr[0] = arr[0], arr[i]
# Heapify at root 0 up to position `i`.
_heapify(arr, i, 0)
def test_heap_sort_in_place():
arr = [1, 5, 3, -4, -10, 0]
heap_sort_in_place(arr)
assert arr == [-10, -4, 0, 1, 3, 5], arr
if __name__ == "__main__":
test_heap_sort_in_place()