Skip to content

Commit f3542c2

Browse files
authored
Merge pull request keon#51 from ihchen/heap-sort
Heap Sort
2 parents ab9dc89 + 00f049f commit f3542c2

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

sort/heap_sort.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
def max_heap_sort(arr):
2+
""" Heap Sort that uses a max heap to sort an array in ascending order
3+
Complexity: O(n log(n))
4+
"""
5+
for i in range(len(arr)-1,0,-1):
6+
max_heapify(arr, i)
7+
8+
temp = arr[0]
9+
arr[0] = arr[i]
10+
arr[i] = temp
11+
12+
13+
def max_heapify(arr, end):
14+
""" Max heapify helper for max_heap_sort
15+
"""
16+
last_parent = int((end-1)/2)
17+
18+
# Iterate from last parent to first
19+
for parent in range(last_parent,-1,-1):
20+
current_parent = parent
21+
22+
# Iterate from current_parent to last_parent
23+
while current_parent <= last_parent:
24+
# Find greatest child of current_parent
25+
child = 2*current_parent + 1
26+
if child + 1 <= end and arr[child] < arr[child+1]:
27+
child = child + 1
28+
29+
# Swap if child is greater than parent
30+
if arr[child] > arr[current_parent]:
31+
temp = arr[current_parent]
32+
arr[current_parent] = arr[child]
33+
arr[child] = temp
34+
35+
current_parent = child
36+
# If no swap occured, no need to keep iterating
37+
else:
38+
break
39+
40+
41+
def min_heap_sort(arr):
42+
""" Heap Sort that uses a min heap to sort an array in ascending order
43+
Complexity: O(n log(n))
44+
"""
45+
for i in range(0, len(arr)-1):
46+
min_heapify(arr, i)
47+
48+
49+
def min_heapify(arr, start):
50+
""" Min heapify helper for min_heap_sort
51+
"""
52+
# Offset last_parent by the start (last_parent calculated as if start index was 0)
53+
# All array accesses need to be offet by start
54+
end = len(arr)-1
55+
last_parent = int((end-start-1)/2)
56+
57+
# Iterate from last parent to first
58+
for parent in range(last_parent,-1,-1):
59+
current_parent = parent
60+
61+
# Iterate from current_parent to last_parent
62+
while current_parent <= last_parent:
63+
# Find lesser child of current_parent
64+
child = 2*current_parent + 1
65+
if child + 1 <= end-start and arr[child+start] > arr[child+1+start]:
66+
child = child + 1
67+
68+
# Swap if child is less than parent
69+
if arr[child+start] < arr[current_parent+start]:
70+
temp = arr[current_parent+start]
71+
arr[current_parent+start] = arr[child+start]
72+
arr[child+start] = temp
73+
74+
current_parent = child
75+
# If no swap occured, no need to keep iterating
76+
else:
77+
break
78+
79+
if __name__ == '__main__':
80+
import timeit
81+
82+
array = [1,5,65,23,57,1232,-1,-5,-2,242,100,4,423,2,564,9,0,10,43,64]
83+
print("array:")
84+
print(array)
85+
print("Max Heapify:")
86+
max_heap_sort(array)
87+
print(array)
88+
array = [1,5,65,23,57,1232,-1,-5,-2,242,100,4,423,2,564,9,0,10,43,64]
89+
print("Min Heapify:")
90+
min_heap_sort(array)
91+
print(array)
92+
print("Max Heapify Time:", timeit.timeit('max_heap_sort(array)', setup="from __main__ import max_heap_sort, array",number=10000))
93+
print("Min Heapify Time:", timeit.timeit('min_heap_sort(array)', setup="from __main__ import min_heap_sort, array",number=10000))

0 commit comments

Comments
 (0)