-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
436 lines (380 loc) · 12.9 KB
/
main.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
import pygame
import random
class Rect:
def __init__(self, x, y, width, height):
self.X = x
self.Y = y
self.width = width
self.height = height
self.colour = BLACK
def show(self):
pygame.draw.rect(screen, self.colour, (self.X, self.Y, self.width, self.height))
def changeCol(self, colour):
self.colour = colour
pygame.init()
screen = pygame.display.set_mode((1000, 700))
pygame.display.set_caption("SORTING VISUALS")
WHITE = (0, 0, 0)
BLACK = (255, 255, 255)
GREEN = (0, 255, 0)
PURPLE = (148, 0, 211)
RED = (255, 0, 0)
BLUE = (105, 152, 255)
L = []
rect = []
# /*-------------------------------------- MERGE SORT FUNCTION----------------------------------------------------*/
def refresh(n):
global rect
screen.fill(WHITE)
for i in range(n):
rect[i].show()
pygame.time.delay(0)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit(0)
pygame.display.update()
def merge(l, m, r, n):
global rect
n1 = m - l + 1
n2 = r - m
# create temp arrays
L = []
R = []
# Copy data to temp arrays L[] and R[]
for i in range(n1):
L.append(Rect(rect[l + i].X, rect[l + i].Y, rect[l + i].width, rect[l + i].height))
# L1[i] = Rect(rect[l + 1].X, rect[l + 1].Y, rect[l + 1].width, rect[l + 1].height)
# L1[i] = Rect(0,0,0,0)
# L1[i], rect[l + 1] = rect[l + i], L1[i]
# L1[i].X, rect[l + 1].X = rect[l + i].X, L1[i].X
refresh(n)
# pygame.time.delay(1000)
print("L1 working")
for j in range(n2):
R.append(Rect(rect[m + 1 + j].X, rect[m + 1 + j].Y, rect[m + 1 + j].width, rect[m + 1 + j].height))
# R[j] = Rect(rect[m + 1 + j].X, rect[m + 1 + j].Y, rect[m + 1 + j].width, rect[m + 1 + j].height)
# R[j] = Rect(0,0,0,0)
# R[j], rect[m + 1 + j] = rect[m + 1 + j], R[j]
# R[j].X, rect[m + 1 + j].X = rect[m + 1 + j].X, R[j].X
refresh(n)
# Merge the temp arrays back into arr[l..r]
i = 0 # Initial index of first subarray
j = 0 # Initial index of second subarray
k = l # Initial index of merged subarray
while i < n1 and j < n2:
print("checking merge...")
if L[i].height <= R[j].height:
# rect[k] = Rect(L[i].X, L[i].Y, L[i].width, L[i].height)
rect[k], L[i] = L[i], rect[k]
rect[k].X, L[i].X = L[i].X, rect[k].X
i += 1
rect[k].colour = GREEN
refresh(n)
rect[k].colour = BLACK
else:
# rect[k] = Rect(R[j].X, R[j].Y, R[j].width, R[j].height)
rect[k], R[j] = R[j], rect[k]
rect[k].X, R[j].X = R[j].X, rect[k].X
j += 1
rect[k].colour = GREEN
refresh(n)
rect[k].colour = BLACK
k += 1
refresh(n)
# Copy the remaining elements of L[], if there
# are any
while i < n1:
rect[k], L[i] = L[i], rect[k]
rect[k].X, L[i].X = L[i].X, rect[k].X
i += 1
k += 1
refresh(n)
# Copy the remaining elements of R[], if there
# are any
while j < n2:
rect[k], R[j] = R[j], rect[k]
rect[k].X, R[j].X = R[j].X, rect[k].X
j += 1
k += 1
refresh(n)
def mergesort(l, r, n):
if l < r:
print("came here")
m = (l + r - 1) // 2 # (rect[l].height + rect[r].height - 1) // 2
mergesort(l, m, n)
refresh(n)
mergesort(m + 1, r, n)
refresh(n)
merge(l, m, r, n)
refresh(n)
def MERGESORT_VISUAL(n):
array(n)
WidthOfEachBar = 800 // (n + 1)
for i in range(n):
x = (i + 1) * (1 + WidthOfEachBar)
y = 680 - L[i]
rect.append(Rect(x, y, WidthOfEachBar, L[i]))
sorted_merge = False
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(WHITE)
if not sorted_merge:
print("entered")
mergesort(0, n - 1, n)
sorted_merge = True
for no1 in range(n):
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit(0)
rect[no1].colour = PURPLE
rect[no1].show()
pygame.time.delay(0)
pygame.display.update()
for no1 in range(n):
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit(0)
rect[no1].colour = PURPLE
rect[no1].show()
pygame.display.update()
# /*-------------------------------------- END OF MERGE SORT FUNCTION------------------------------------------------*/
# /*-------------------------------------- INSERTION SORT FUNCTION----------------------------------------------------*/
def equal(b):
a = Rect(0, 0, 0, 0)
a.X = b.X
a.Y = b.Y
a.width = b.width
a.height = b.height
a.colour = b.colour
return a
def insertionsort(n):
global rect
for oo in range(n):
print(rect[oo].height, end=" ")
print()
for i in range(1, n):
print("outer loop")
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit(0)
rect[i].colour = GREEN
key = rect[i]
key.X = rect[i].X
j = i - 1
while j >= 0 and key.height < rect[j].height:
for oo in range(n):
print(rect[oo].height, end=" ")
print()
rect[j + 1], rect[j] = rect[j], rect[j + 1]
rect[j + 1].X, rect[j].X = rect[j].X, rect[j + 1].X
j = j - 1
rect[j + 1], key = key, rect[j + 1]
rect[j + 1].X, key.X = key.X, rect[j + 1].X
screen.fill(WHITE)
for a in range(n):
rect[a].show()
pygame.display.update()
rect[j + 1].colour = BLACK
def INSERTION_SORT_VISUAL(n):
array(n)
WidthOfEachBar = 800 // (n + 1)
for i in range(n):
x = (i + 1) * (1 + WidthOfEachBar)
y = 680 - L[i]
rect.append(Rect(x, y, WidthOfEachBar, L[i]))
sorted_insertion = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit(0)
if not sorted_insertion:
insertionsort(n)
for oo in range(n):
print(rect[oo].height, end=" ")
print()
print("came once")
sorted_insertion = True
for a in range(n):
rect[a].colour = PURPLE
rect[a].show()
pygame.display.update()
pygame.display.update()
# /*------------------------------- END OF INSERTION SORT FUNCTION----------------------------------------------------*/
# /*-------------------------------------- QUICK SORT FUNCTION-------------------------------------------------------*/
def partition(low, high, n):
global rect
pivot = rect[high]
pivot, rect[high] = rect[high], pivot
pivot.X, rect[high].X = rect[high].X, pivot.X
pivot.colour = RED
i = low - 1
for j in range(low, high):
if rect[j].height < pivot.height:
i += 1
rect[i].colour = GREEN
rect[j].colour = GREEN
swap(i, j)
refresh(n)
rect[i].colour = BLACK
rect[j].colour = BLACK
rect[high].colour = RED
rect[i + 1].colour = RED
swap(high, i + 1)
refresh(n)
rect[high].colour = BLACK
rect[i + 1].colour = BLACK
return i + 1
def quicksort(low, high, n):
global rect
if low < high:
pi = partition(low, high, n) # pi is pivot element
quicksort(low, pi - 1, n)
refresh(n)
quicksort(pi + 1, high, n)
refresh(n)
def QSORT_VISUAL(n):
array(n)
WidthOfEachBar = 800 // (n + 1)
for i in range(n):
x = (i + 1) * (1 + WidthOfEachBar)
y = 680 - L[i]
rect.append(Rect(x, y, WidthOfEachBar, L[i]))
sorted_quick = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit(0)
# print("calling q sort")
if not sorted_quick:
quicksort(0, n - 1, n)
refresh(n)
sorted_quick = True
for i in range(n):
rect[i].colour = PURPLE
rect[i].show()
pygame.time.delay(0)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit(0)
pygame.display.update()
# /*------------------------------------END OF QUICKSORT FUNCTION----------------------------------------------------*/
# /*-------------------------------------- BUBBLE SORT FUNCTION-------------------------------------------------------*/
def swap(a, b):
global rect
rect[a], rect[b] = rect[b], rect[a]
rect[a].X, rect[b].X = rect[b].X, rect[a].X
def bubble_sort(n):
global rect
for ii1 in range(n):
for j in range(n - 1):
rect[j].colour = GREEN
rect[j + 1].colour = GREEN
if rect[j].height > rect[j + 1].height:
# print(r[j].X, r[j + 1].X)
swap(j, j + 1)
# print(r[j].X, r[j + 1].X)
for amb in range(n):
print(rect[amb].height, end=" ")
print()
screen.fill(WHITE)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit(0)
for no1 in range(n):
rect[no1].show()
pygame.time.delay(0)
pygame.display.update()
rect[j].colour = BLACK
rect[j + 1].colour = BLACK
print("DONE")
def BBSORRT_VISUAL(n):
array(n)
WidthOfEachBar = 800 // (n + 1)
# initialise the rectangles...
for i in range(n):
x = (i + 1) * (1 + WidthOfEachBar)
y = 680 - L[i]
rect.append(Rect(x, y, WidthOfEachBar, L[i]))
sorted_bubble = False
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(WHITE)
if not sorted_bubble:
bubble_sort(n)
sorted_bubble = True
for no1 in range(n):
rect[no1].colour = PURPLE
rect[no1].show()
pygame.time.delay(0)
pygame.display.update()
pygame.display.update()
# /*---------------------------------------------END OF BUBBLESORT----------------------------------------------------*/
n = 100
def array(n_):
global L
L.clear()
arr = [(3 * i) for i in range(1, n_ + 1)]
for a in range(n_):
random_no = random.choice(arr)
L.append(random_no + 10)
arr.remove(random_no)
def create_new_array(n):
array(n)
rect.clear()
WidthOfEachBar = 800 // (n + 1)
for i in range(n):
x = (i + 1) * (1 + WidthOfEachBar)
y = 680 - L[i]
rect.append(Rect(x, y, WidthOfEachBar, L[i]))
refresh(n)
create_new_array(n)
sorted_quick = True
sorted_merge = True
sorted_bubble = True
sorted_insertion = True
while True:
# print("enter options")
screen.fill(WHITE)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit(0)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_b:
bubble_sort(n)
elif event.key == pygame.K_q:
quicksort(0, n - 1, n)
elif event.key == pygame.K_i:
insertionsort(n)
elif event.key == pygame.K_m:
mergesort(0, n - 1, n)
elif event.key == pygame.K_r:
create_new_array(n)
refresh(n)
elif event.key == pygame.K_UP:
if n <= 220:
n = n + 30
print("up pressed")
screen.fill(WHITE)
create_new_array(n)
refresh(n)
elif event.key == pygame.K_DOWN:
if n >= 40:
n = n - 30
print("down pressed")
screen.fill(WHITE)
create_new_array(n)
refresh(n)
for i in range(n):
rect[i].colour = PURPLE
rect[i].show()
pygame.time.delay(0)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit(0)
pygame.display.update()