-
Notifications
You must be signed in to change notification settings - Fork 0
/
HeapSort-Procedural.py
175 lines (153 loc) · 5.3 KB
/
HeapSort-Procedural.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
"""
Uses 0-based indexing.
Doesn't use OOP, that is, classes. It's in procedural paradigm.
Also includes functions for Partial Sorting.
"""
def parent(i):
return (i - 1)//2
def leftChild(i):
return 2*i + 1
def rightChild(i):
return 2*i + 2
def siftDownMax(H, i, size):
"""Sifts the element with index i down until it finds its place.
Taken from MaxHeap class.
O(log n)
"""
while i < size:
maxIndex = i
l = leftChild(i)
if l < size and H[l] > H[maxIndex]:
maxIndex = l
r = rightChild(i)
if r < size and H[r] > H[maxIndex]:
maxIndex = r
if i != maxIndex:
H[i], H[maxIndex] = H[maxIndex], H[i]
i = maxIndex
else:
break
def siftDownMin(H, i, size):
"""Sifts the element with index i down until it finds its place.
Taken from MinHeap class.
O(log n)
"""
while i < size:
maxIndex = i
l = leftChild(i)
if l < size and H[l] < H[maxIndex]:
maxIndex = l
r = rightChild(i)
if r < size and H[r] < H[maxIndex]:
maxIndex = r
if i != maxIndex:
H[i], H[maxIndex] = H[maxIndex], H[i]
i = maxIndex
else:
break
def extractMax(H, size):
"""Returns the element with the highest value (priority).
Taken from MaxHeap class.
O(log n)"""
if size >= 1:
result = H[0]
H[0] = H[size-1]
size -= 1
siftDownMax(H, 0, size)
return result
else:
raise Exception("The heap is empty! Cannot return the element with the highest value.")
def extractMin(H, size):
"""Returns the element with the lowest value (priority).
Taken from MinHeap class.
O(log n)"""
if size >= 1:
result = H[0]
H[0] = H[size-1]
size -= 1
siftDownMin(H, 0, size)
return result
else:
raise Exception("The heap is empty! Cannot return the element with the lowest value.")
def buildHeapMax(A, n):
"""Turns the given array A with length n into a heap. Works in-place.
Inputs: A, n
Its time complexity is actually O(2n), which is O(n), and it doesn't use additional space.
"""
size = n # n = len(A)
for i in range(n//2, -1, -1):
siftDownMax(A, i, size)
def buildHeapMin(A, n):
"""Turns the given array A with length n into a heap. Works in-place.
Inputs: A, n
Its time complexity is actually O(2n), which is O(n), and it doesn't use additional space.
"""
size = n # n = len(A)
for i in range(n//2, -1, -1):
siftDownMin(A, i, size)
def heapSortNonDescending(A, n):
"""Turns the given array A with length n into a heap, and then it sorts it in-place.
It doesn't return A, because it sorts it in-place.
Its time complexity is O(n log n), and it doesn't use additional space.
"""
buildHeapMax(A, n)
size = n
for _ in range(n):
A[0], A[size-1] = A[size-1], A[0]
size -= 1
siftDownMax(A, 0, size)
def heapSortNonAscending(A, n):
"""Turns the given array A with length n into a heap, and then it sorts it in-place.
It doesn't return A, because it sorts it in-place.
Its time complexity is O(n log n), and it doesn't use additional space.
"""
buildHeapMin(A, n)
size = n
for _ in range(n):
A[0], A[size-1] = A[size-1], A[0]
size -= 1
siftDownMin(A, 0, size)
def partialSortingMax(A, n, k):
"""Inputs: Array A[1, n]; n, which is len(A); Integer k, such that 1 <= k <= n
Output: The last (maximal) k elements of a sorted version of A.
O(n), if k <= O(n/log n)
"""
assert 1 <= k <= n
A = A[:] # This works! This is if we want to preserve the input array A - otherwise, it gets destroyed.
buildHeapMax(A, n)
result = []
for i in range(k):
result.append(extractMax(A, n-i))
return result
def partialSortingMin(A, n, k):
"""Inputs: Array A[1, n]; n, which is len(A); Integer k, such that 1 <= k <= n
Output: The last (minimal) k elements of a sorted version of A.
O(n), if k <= O(n/log n)
"""
assert 1 <= k <= n
A = A[:] # This works! This is if we want to preserve the input array A - otherwise, it gets destroyed.
buildHeapMin(A, n)
result = []
for i in range(k):
result.append(extractMin(A, n-i))
return result
if __name__ == "__main__":
elts = [11, 13, 12, 18, 14, 42, 7, 18, 29] # len(elts) = 9
n = len(elts)
k = n # Test with boundary cases, especially with k = n.
A = elts[:]
heapSortNonDescending(A, n) # Sorts the array in place! A will be sorted after this!
print(A, elts)
print()
#A = elts[:]
heapSortNonAscending(A, n) # Sorts the array in place! A will be sorted after this!
print(A, elts)
print()
#A = elts[:]
print(partialSortingMax(A, n, k))
print(A, elts)
print()
#A = elts[:]
print(partialSortingMin(A, n, k))
print(A, elts)
print()