Skip to content

Commit 2a7de6b

Browse files
improved visualization and algs
1 parent 13d57a1 commit 2a7de6b

14 files changed

+90
-49
lines changed
246 Bytes
Binary file not shown.
592 Bytes
Binary file not shown.
142 Bytes
Binary file not shown.
154 Bytes
Binary file not shown.

Algorithms/bubble_sort.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,42 @@
11
from Display.display import update_display
2+
from Helper.global_variables import *
3+
import winsound
4+
import math
25

36
def bubble_sort_swap(index, height, numswaps):
4-
7+
58
aux = height[index]
69
height[index] = height[index+1]
710
height[index+1] = aux
811
numswaps = numswaps + 1
9-
return numswaps
12+
return numswaps
1013

11-
def bubble_sort(win, height, numswaps, algorithm, number_of_elements, speed):
14+
def bubble_sort(win, height, color_height, numswaps, algorithm, number_of_elements, speed):
1215

1316
swap_occurred = 0;
17+
n = len(height)
1418

15-
for i in range(len(height)-1):
16-
17-
for j in range(len(height)-1):
19+
for i in range(n-1):
1820

19-
if(height[i] > height[i+1]):
21+
for j in range(0, n-i-1):
22+
23+
color_height[j] = GREEN
24+
color_height[j+1] = GREEN
25+
update_display(win, height, color_height, numswaps, algorithm, number_of_elements, speed, 0, True)
2026

21-
swap_occurred = 1;
27+
if(height[j] > height[j+1]):
2228

23-
numswaps = bubble_sort_swap(i, height, numswaps)
24-
update_display(win, height, numswaps, algorithm, number_of_elements, speed, 0)
25-
#update_display()
29+
color_height[j] = RED
30+
color_height[j+1] = RED
31+
numswaps = bubble_sort_swap(j, height, numswaps)
32+
freq = height[j] - height[j+1]
33+
#winsound.Beep(abs(freq)*10 + 37, 50)
34+
update_display(win, height, color_height, numswaps, algorithm, number_of_elements, speed, 0, True)
35+
36+
color_height[j] = TURQUOISE
37+
color_height[j+1] = TURQUOISE
38+
color_height[j+1] = PURPLE
2639

27-
if(swap_occurred == 1):
28-
bubble_sort(win, height, numswaps, algorithm, number_of_elements, speed)
40+
color_height[0] = PURPLE
2941

3042
return numswaps

Algorithms/insertion_sort.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
from Display.display import update_display
2+
from Helper.global_variables import *
23

3-
def insertion_sort(win, height, numswaps, algorithm, number_of_elements, speed):
4+
def insertion_sort(win, height, color_height, numswaps, algorithm, number_of_elements, speed):
45

56
# Traverse through 1 to len(height)
67
for i in range(1, len(height)):
78

89
key = height[i]
10+
#color_height[i] = GREEN
911

1012
# Move elements of height[0..i-1], that are
1113
# greater than key, to one position ahead
1214
# of their current position
1315
j = i-1
1416
while j >= 0 and key < height[j] :
17+
color_height[i] = GREEN
1518
height[j + 1] = height[j]
1619
j -= 1
1720
numswaps = numswaps + 1
18-
update_display(win, height, numswaps, algorithm, number_of_elements, speed, 0)
19-
height[j + 1] = key
21+
color_height[j+1] = RED
22+
color_height[j] = RED
23+
update_display(win, height, color_height, numswaps, algorithm, number_of_elements, speed, 0, True)
24+
color_height[j+1] = TURQUOISE
25+
color_height[j] = TURQUOISE
26+
height[j + 1] = key
27+
28+
return numswaps

Algorithms/selection_sort.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from Display.display import update_display
2+
from Helper.global_variables import *
23

34
def selection_sort_swap(index1, index2, height, numswaps):
45

@@ -12,17 +13,31 @@ def selection_sort_swap(index1, index2, height, numswaps):
1213
#else:
1314
# winsound.Beep(int(height[index2]/height[index1])*100, 100)
1415

15-
def selection_sort(win, height, numswaps, algorithm, number_of_elements, speed):
16+
def selection_sort(win, height, color_height, numswaps, algorithm, number_of_elements, speed):
1617

1718
for i in range(0, len(height) -1):
1819

1920
min = i
2021

22+
color_height[i] = GREEN
2123
for j in range(i+1, len(height)):
2224

25+
#color_height[j] = GREEN
26+
#color_height[min] = GREEN
27+
#update_display(win, height, color_height, numswaps, algorithm, number_of_elements, speed, 0, True)
28+
2329
if(height[j] < height[min]):
2430

31+
color_height[j] = RED
32+
#color_height[min] = RED
2533
min = j
2634

27-
numswaps = selection_sort_swap(min, i, height, numswaps)
28-
update_display(win, height, numswaps, algorithm, number_of_elements, speed, 0)
35+
numswaps = selection_sort_swap(min, i, height, numswaps)
36+
update_display(win, height, color_height, numswaps, algorithm, number_of_elements, speed, 0, True)
37+
38+
for k in range(0, i+1):
39+
color_height[k] = PURPLE
40+
41+
color_height[len(height)-1] = PURPLE
42+
43+
return numswaps
38 Bytes
Binary file not shown.

Display/display.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
pygame.init()
66

7-
def update_display(win, height, numswaps, algorithm, number_of_elements, speed, time):
7+
def update_display(win, height, color_height, numswaps, algorithm, number_of_elements, speed, time, running):
88

99
win.fill(BLACK)
1010

1111
# call show method to display the list items
12-
show(win, height, number_of_elements)
12+
show(win, height, color_height, number_of_elements)
1313

1414
for i in range(15):
1515
pygame.draw.line(win, TURQUOISE, (0, 165+i), (WIDTH, 165+i))
@@ -43,34 +43,30 @@ def update_display(win, height, numswaps, algorithm, number_of_elements, speed,
4343
button_instant.draw(win)
4444

4545
# create a time delay
46-
delay = 0
47-
if(speed == "Slow"):
48-
delay = 50
49-
if(speed == "Medium"):
50-
delay = 25
51-
if(speed == "Fast"):
52-
delay = 10
53-
if(speed == "No delay"):
46+
if(running == True):
5447
delay = 0
55-
pygame.time.delay(delay)
48+
if(speed == "Slow"):
49+
delay = 1000
50+
if(speed == "Medium"):
51+
delay = 50
52+
if(speed == "Fast"):
53+
delay = 25
54+
if(speed == "No delay"):
55+
delay = 1
56+
pygame.time.delay(delay)
5657

5758
# update the display
5859
pygame.display.update()
5960

6061
# method to show the list of height
61-
def show(win, height, number_of_elements):
62-
62+
def show(win, height, color_height, number_of_elements):
63+
6364
if(number_of_elements != -1 and len(height) != 0):
6465

6566
maximum_value = max(height)
6667
step = (WIDTH/len(height))
6768

6869
for i in range(len(height)):
69-
70-
x = Button(step * (i+1), HEIGHT, -(step-10), -(height[i]/maximum_value)*3*HEIGHT/4, BLACK, TURQUOISE, str(height[i]), int(round(step - 20)))
71-
x.draw(win)
72-
#font = pygame.font.SysFont('Arial', int(round(step - 13)))
73-
#text = font.render(str(height[i]), False, (255, 0, 0))
74-
#rectangle = pygame.draw.rect(win, (255, 255, 0), (step * (i+1), HEIGHT, -(step-10), -(height[i]/maximum_value)*3*HEIGHT/4))
75-
#text_rect = text.get_rect(center=(step * (i+1) - (step-10)/2, HEIGHT - 10))
76-
#win.blit(text, text_rect)
70+
71+
x = Button(step * (i+1), HEIGHT, -(step), -(height[i]/maximum_value)*3*HEIGHT/4, BLACK, color_height[i], str(height[i]), int(round(step - 20)))
72+
x.draw(win)
18 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)