-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSorting Algorithm.py
143 lines (108 loc) · 3.85 KB
/
Sorting Algorithm.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
import pygame, random
pygame.init()
# WINDOW
WINDOW_SIZE = 600
WINDOW = pygame.display.set_mode((WINDOW_SIZE, WINDOW_SIZE))
pygame.display.set_caption('Sorting Algorithm Visualization')
# VARIABLES
RECT_WIDTH = 20
clock = pygame.time.Clock()
FPS = 10
# COLORS
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
PURPLE = (128, 0, 128)
YELLOW = (255,255,0)
RED = (255,0 ,0)
# CLASSES
class Rectangle:
def __init__(self, color, x, height):
self.color = color
self.x = x
self.width = RECT_WIDTH
self.height = height
def select(self):
self.color = BLUE
def unselect(self):
self.color = PURPLE
def set_smallest(self):
self.color = RED
def set_sorted(self):
self.color = GREEN
# FUNCTIONS
def create_rectangles():
num_rectangles = WINDOW_SIZE // RECT_WIDTH - 5
rectangles = []
heights = []
for i in range(5, num_rectangles):
height = random.randint(20, 500)
while height in heights:
height = random.randint(20, 500)
heights.append(height)
rect = Rectangle(PURPLE, i*RECT_WIDTH, height)
rectangles.append(rect)
return rectangles
def draw_rects(rectangles):
WINDOW.fill(YELLOW)
for rect in rectangles:
pygame.draw.rect(WINDOW, rect.color, (rect.x, WINDOW_SIZE-rect.height, rect.width, rect.height))
pygame.draw.line(WINDOW, BLACK, (rect.x, WINDOW_SIZE), (rect.x, WINDOW_SIZE-rect.height))
pygame.draw.line(WINDOW, BLACK, (rect.x+rect.width, WINDOW_SIZE), (rect.x+rect.width, WINDOW_SIZE-rect.height))
pygame.draw.line(WINDOW, BLACK, (rect.x, WINDOW_SIZE-rect.height), (rect.x+rect.width, WINDOW_SIZE-rect.height))
def selection_sort(rectangles):
num_rectangles = len(rectangles)
for i in range(num_rectangles):
min_index = i
rectangles[i].set_smallest()
for j in range(i+1, num_rectangles):
rectangles[j].select()
draw_rects(rectangles)
if rectangles[j].height < rectangles[min_index].height:
rectangles[min_index].unselect()
min_index = j
rectangles[min_index].set_smallest()
draw_rects(rectangles)
rectangles[j].unselect()
yield
rectangles[i].x, rectangles[min_index].x = rectangles[min_index].x, rectangles[i].x
rectangles[i], rectangles[min_index] = rectangles[min_index], rectangles[i]
rectangles[min_index].unselect()
rectangles[i].set_sorted()
draw_rects(rectangles)
def display_text(txt, y, size):
FONT = pygame.font.SysFont('Futura', size)
text = FONT.render(txt, True, BLACK)
text_rect = text.get_rect(center=(WINDOW_SIZE/2, y))
WINDOW.blit(text, text_rect)
# MAIN FUNCTION
def main():
rectangles = create_rectangles()
draw_rects(rectangles)
sorting_generator = selection_sort(rectangles)
# MAIN LOOP
run = True
sorting = False
while run:
clock.tick(FPS)
if sorting:
try:
next(sorting_generator)
except StopIteration:
sorting = False
else:
draw_rects(rectangles)
display_text('Sorting Algorithm Visualization: ', 30, 40)
display_text('Press SPACE to start sorting or pause or q to quit.', 70, 30)
# EVENT HANDLER
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
sorting = not sorting
if event.key == pygame.K_q:
run = False
pygame.display.update()
pygame.quit()
main()