-
Notifications
You must be signed in to change notification settings - Fork 0
/
heapSort.py
65 lines (49 loc) · 1.72 KB
/
heapSort.py
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
"""
[Lecture 04.]
Heaps ans Heap Sort
1. Max Heapify
- Time Complexity: 𝛩(n)
2. Heap Sort
- Time Complexity: 𝛩(nlogn)
- n iterations, 𝛩(logn) for every iteration
"""
from typing import List
def heapify(arr: List[int], parent: int, tail_node: int) -> None:
"""
Time Complexity: 𝛩(logn)
"""
left_child = 2 * parent + 1
right_child = 2 * parent + 2
# See if left child of root exists and is greater than parent
if left_child < tail_node and arr[parent] < arr[left_child]:
arr[parent], arr[left_child] = arr[left_child], arr[parent] # Swap
# See if right child of root exists and is greater than parent
if right_child < tail_node and arr[parent] < arr[right_child]:
arr[parent], arr[right_child] = arr[right_child], arr[parent] # Swap
# Heapify the children
if left_child < tail_node // 2:
heapify(arr, left_child, tail_node)
if right_child < tail_node // 2:
heapify(arr, right_child, tail_node)
def max_heapify(arr: List[int]) -> None:
"""
Time Complexity: 𝛩(n)
"""
for i in range(len(arr)//2 - 1, -1, -1):
heapify(arr, i, len(arr))
def heap_sort(arr: List[int]) -> None:
"""
Time Complexity: 𝛩(nlogn)
"""
# Step1. Build Max_Heap from unordered array
max_heapify(arr)
# Step4. Iterate
for i in range(len(arr)-1, 0, -1):
arr[i], arr[0] = arr[0], arr[i] # Step2. Find Maximum element and swap to last node
heapify(arr, 0, i) # Step3. discard last node from heap and heapify
def main():
arr = [1, 4, 2, 3, 9, 7, 8, 10, 14, 16]
heap_sort(arr)
print(arr)
if __name__ == "__main__":
main()