-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorting-agorithm.py
More file actions
165 lines (121 loc) · 4.68 KB
/
sorting-agorithm.py
File metadata and controls
165 lines (121 loc) · 4.68 KB
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
import pygame
import random
import math
pygame.init()
class ColorInformation:
BLACK = 0, 0, 0
SOFTWHITE = 250, 250, 250
BLUE = 0, 0, 255
GREEN = 0, 255, 0
LIGHTGREY = 192, 192, 192
GREY = 110, 110, 110
PADDINGPIX = 200
FONT = pygame.font.SysFont('dejavusansmono', 20)
def __init__(self, width, height, unsorted_list):
self.width = width
self.height = height
self.window = pygame.display.set_mode((width, height))
pygame.display.set_caption("SORTING ALGORITHM VISUALISER")
self.set_list(unsorted_list)
def set_list(self, unsorted_list):
self.unsorted_list = unsorted_list
self.max = max(unsorted_list)
self.min = min(unsorted_list)
self.bar_width = round((self.width - self.PADDINGPIX) / len(unsorted_list))
self.bar_height = math.floor((self.height - self.PADDINGPIX) / (self.max - self.min))
self.start_cor_x = self.PADDINGPIX // 2
def generate_random_list(len, min_val, max_val):
lst = []
for _ in range(len):
lst.append(random.randint(min_val, max_val))
return lst
def draw(color_info):
color_info.window.fill(color_info.SOFTWHITE)
keycontrols = color_info.FONT.render("Hit the SPACEBAR to start sorting | R - Reset", 1, color_info.BLACK)
color_info.window.blit(keycontrols, (5, 5))
draw_list(color_info)
pygame.display.update()
def draw_list(color_info, color_position=dict(), clear_plot=False):
lst = color_info.unsorted_list
if clear_plot:
rect_to_clear = (color_info.PADDINGPIX // 2,
color_info.PADDINGPIX,
color_info.width - color_info.PADDINGPIX,
color_info.height - color_info.PADDINGPIX)
pygame.draw.rect(color_info.window, color_info.SOFTWHITE, rect_to_clear)
for i, val in enumerate(lst):
x_point = color_info.start_cor_x + i * color_info.bar_width
y_point = color_info.height - (val - color_info.min) * color_info.bar_height
color = color_info.GREY
if i % 2 == 1:
color = color_info.LIGHTGREY
if i in color_position:
color = color_position[i]
pygame.draw.rect(color_info.window, color, (x_point, y_point, color_info.bar_width, color_info.height))
if clear_plot:
pygame.display.update()
def main():
run = True
clock = pygame.time.Clock()
n = 30
min_val = 0
max_val = 100
unsorted_list = generate_random_list(n, min_val, max_val)
color_info = ColorInformation(800, 600, unsorted_list)
currently_sorting = False
current_algorithm = bubble_sort
current_algorithm_name = "bubble sort"
current_generator = None
while run:
clock.tick(60)
if currently_sorting:
try:
next(current_generator)
except StopIteration:
sorting = False
else:
draw(color_info)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type != pygame.KEYDOWN:
continue
if event.key == pygame.K_SPACE:
currently_sorting = True
current_generator = current_algorithm(color_info)
if event.key == pygame.K_r:
unsorted_list = generate_random_list(n, min_val, max_val)
color_info.set_list(unsorted_list)
currently_sorting = False
if event.key == pygame.K_i:
current_algorithm = insertion_sort
current_algorithm_name = "insertion sort"
if event.key == pygame.K_b:
current_algorithm = bubble_sort
current_algorithm_name = "bubble sort"
pygame.quit()
def bubble_sort(color_info):
unsorted_list = color_info.unsorted_list
for i in range(len(unsorted_list) - 1):
for j in range(len(unsorted_list) - i - 1):
val1 = unsorted_list[j]
val2 = unsorted_list[j + 1]
if val1 > val2:
unsorted_list[j], unsorted_list[j + 1] = unsorted_list[j + 1], unsorted_list[j]
draw_list(color_info, {j: color_info.BLUE, j + 1: color_info.GREEN}, True)
yield True
draw_list(color_info)
return unsorted_list
def insertion_sort(color_info):
lst = color_info.unsorted_list
for i in range(1, len(lst)):
val = lst[i]
while lst[i - 1] > val and i != 0:
lst[i] = lst[i - 1]
i -= 1
lst[i] = val
draw_list(color_info, {i - 1: color_info.BLUE, i: color_info.GREEN}, True)
yield True
return lst
if __name__ == "__main__":
main()