-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheapsort.py
More file actions
67 lines (58 loc) · 1.68 KB
/
heapsort.py
File metadata and controls
67 lines (58 loc) · 1.68 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
63
64
65
66
67
def _get_left_index(index):
return (index << 1) + 1
def _get_right_index(index):
return (index + 1) << 1
def _get_parent_index(index):
return (index-1) >> 1
def _get_min_child_index(arr, index, L):
li = _get_left_index(index)
ri = _get_right_index(index)
if ri < L:
if arr[li] < arr[ri]:
return li
else:
return ri
elif li < L:
return li
else:
return None
def _swap(arr, i, j):
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
def heapify(arr, index, L):
next_index = _get_min_child_index(arr, index, L)
#print("next index {}".format(next_index))
if next_index is None:
return
else:
if arr[index] > arr[next_index]:
_swap(arr, index, next_index)
return heapify(arr, next_index, L)
def build_min_heap(arr):
index = _get_parent_index(len(arr)-1) # Parent of last node i,e index len(arr)-1
L = len(arr)
while(index >= 0):
heapify(arr, index, L)
index -= 1
def heapsort(arr):
build_min_heap(arr)
index = len(arr) - 1
while(index > 0):
_swap(arr, 0, index)
heapify(arr, 0, index) # Trap Alert: You can tempted to pass the slice arr[:index] here but swaps won't reflect since python
# passed around a new / different referece. So pass the full arr instead !
index -= 1
return arr
if __name__ == "__main__":
arr = [9,4,3,8,10,2,5]
print(arr)
print(heapsort(arr))
print("------------------")
arr = [1,2,3,3,4,5,6]
print(arr)
print(heapsort(arr))
print("------------------")
arr = [6,5,4,3,2,1,0]
print(arr)
print(heapsort(arr))