Skip to content

Commit 54cb3f4

Browse files
added heap sort
1 parent cae84bb commit 54cb3f4

File tree

5 files changed

+60
-2
lines changed

5 files changed

+60
-2
lines changed
998 Bytes
Binary file not shown.
38 Bytes
Binary file not shown.

Algorithms/heap_sort.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from Display.display import update_display
2+
from Helper.global_variables import *
3+
4+
# To heapify subtree rooted at index i.
5+
# n is size of heap
6+
def heapify(height, n, i, color_height, win, numswaps, algorithm, number_of_elements, speed):
7+
largest = i # Initialize largest as root
8+
l = 2 * i + 1 # left = 2*i + 1
9+
r = 2 * i + 2 # right = 2*i + 2
10+
11+
# See if left child of root exists and is
12+
# greater than root
13+
if l < n and height[i] < height[l]:
14+
largest = l
15+
16+
# See if right child of root exists and is
17+
# greater than root
18+
if r < n and height[largest] < height[r]:
19+
largest = r
20+
21+
# Change root, if needed
22+
if largest != i:
23+
height[i],height[largest] = height[largest],height[i] # swap
24+
update_display(win, height, color_height, numswaps, algorithm, number_of_elements, speed, 0, True)
25+
# Heapify the root.
26+
heapify(height, n, largest, color_height, win, numswaps, algorithm, number_of_elements, speed)
27+
28+
# The main function to sort an heightay of given size
29+
def heap_sort(height, color_height, win, numswaps, algorithm, number_of_elements, speed):
30+
n = len(height)
31+
32+
# Build a maxheap.
33+
# Since last parent will be at ((n//2)-1) we can start at that location.
34+
for i in range(n // 2 - 1, -1, -1):
35+
heapify(height, n, i, color_height, win, numswaps, algorithm, number_of_elements, speed)
36+
37+
# One by one extract elements
38+
for i in range(n-1, 0, -1):
39+
height[i], height[0] = height[0], height[i] # swap
40+
heapify(height, i, 0, color_height, win, numswaps, algorithm, number_of_elements, speed)

Algorithms/insertion_sort.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,7 @@ def insertion_sort(win, height, color_height, numswaps, algorithm, number_of_ele
2525
color_height[j] = TURQUOISE
2626
height[j + 1] = key
2727

28+
for i in range(len(color_height)):
29+
color_height[i] = PURPLE
30+
2831
return numswaps

main.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from Algorithms.selection_sort import selection_sort
1313
from Algorithms.insertion_sort import insertion_sort
1414
from Algorithms.merge_sort import merge_sort
15+
from Algorithms.heap_sort import heap_sort
1516
from Display.display import update_display, show
1617

1718
pygame.init()
@@ -78,6 +79,11 @@ def main(win):
7879
else:
7980
button_selection_sort.background_color = DARK_BUTTON
8081

82+
if(button_heap_sort.check() == True):
83+
button_heap_sort.background_color = LIGHT_BUTTON
84+
else:
85+
button_heap_sort.background_color = DARK_BUTTON
86+
8187
if(button_merge_sort.check() == True):
8288
button_merge_sort.background_color = LIGHT_BUTTON
8389
else:
@@ -143,13 +149,19 @@ def main(win):
143149

144150
if(algorithm == "Insertion Sort"):
145151
start_time = time.time()
146-
numswaps = insertion_sort(win, height, color_height, 0, algorithm, number_of_elements, speed)
152+
current_numswaps = insertion_sort(win, height, color_height, 0, algorithm, number_of_elements, speed)
147153
elapsed_time = time.time() - start_time
148154
update_display(win, height, color_height, current_numswaps, algorithm, number_of_elements, speed, elapsed_time, False)
149155

150156
if(algorithm == "Selection Sort"):
151157
start_time = time.time()
152-
numswaps = selection_sort(win, height, color_height, 0, algorithm, number_of_elements, speed)
158+
current_numswaps = selection_sort(win, height, color_height, 0, algorithm, number_of_elements, speed)
159+
elapsed_time = time.time() - start_time
160+
update_display(win, height, color_height, current_numswaps, algorithm, number_of_elements, speed, elapsed_time, False)
161+
162+
if(algorithm == "Heap Sort"):
163+
start_time = time.time()
164+
current_numswaps = heap_sort(height, color_height, win, 0, algorithm, number_of_elements, speed)
153165
elapsed_time = time.time() - start_time
154166
update_display(win, height, color_height, current_numswaps, algorithm, number_of_elements, speed, elapsed_time, False)
155167

@@ -174,6 +186,9 @@ def main(win):
174186
if(button_merge_sort.check() == True):
175187
algorithm = "Merge Sort"
176188

189+
if(button_heap_sort.check() == True):
190+
algorithm = "Heap Sort"
191+
177192
if(button_20.check() == True):
178193
number_of_elements = 20
179194

0 commit comments

Comments
 (0)