-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvisualizer.py
173 lines (155 loc) · 6.21 KB
/
visualizer.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
import random
import pygame
import sys
from pygame import Color, display
pygame.init()
disp = pygame.display.set_mode((700, 400))
x = 40
y = 40
bar_width = 20
bar_height = [200, 50, 130, 90, 250, 61, 110, 88, 33, 80, 70, 159, 180, 20, 210, 39, 48, 56, 18, 240, 95]
colors = ['#bcbcbc' ,'#a9dcd6' ,'#260566' ,'#caf0be' ,'#ffd358' ,'#f4a201','#b536f0' ,'#ffba42' ,'#a9dcd6' ,'#c39797' ,'#ff7f50' ,'#acb843' ,'#f9dada' ,'#f6b4be' ,'#ee9b9b','#0a6fd3' ,'#693a7c' ,'#b5b4e5' ,'#abeeaa','#483d8b' ,'#3a95cb']
def draw_bars(h):
for i in range(len(h)):
pygame.draw.rect(disp, colors[i], pygame.Rect(x + (30 * i), y, bar_width, bar_height[i]))
ok = True
while ok:
run = False
pygame.time.delay(10)
any_key_get_pressed = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
ok = False #close pygame window
# if any_key_get_pressed[pygame.K_SPACE]:
if sys.argv[1] != "":
run = True
if run == False:
disp.fill((0, 0, 0))
draw_bars(bar_height)
pygame.display.update()
#algorithm for bubble sort
elif sys.argv[1].lower() == "bubblesort":
pygame.display.set_caption("Bubble Sort")
for i in range(len(bar_height) - 1):
vis = False
for j in range(len(bar_height) - i - 1):
if bar_height[j] > bar_height[j + 1]:
tmp = bar_height[j]
bar_height[j] = bar_height[j + 1]
bar_height[j + 1] = tmp
vis = True
disp.fill((0, 0, 0))
draw_bars(bar_height)
pygame.time.delay(100)
pygame.display.update()
if not vis:
break
elif sys.argv[1].lower() == "selectionsort":
#selection sort here
pygame.display.set_caption("Selection Sort")
for i in range(len(bar_height)):
minNum = i
for j in range(i+1,len(bar_height)):
if bar_height[minNum] > bar_height[j]:
minNum = j
bar_height[i],bar_height[minNum] = bar_height[minNum],bar_height[i]
disp.fill((0, 0, 0))
draw_bars(bar_height)
pygame.time.delay(100)
pygame.display.update()
elif sys.argv[1].lower() == "insertionsort":
#insertion sort here
pygame.display.set_caption("Insertion Sort")
for i in range(1,len(bar_height)):
key = bar_height[i]
j = i - 1
while j >=0 and key < bar_height[j]:
bar_height[j+1] = bar_height[j]
j -= 1
disp.fill((0, 0, 0))
draw_bars(bar_height)
pygame.time.delay(100)
pygame.display.update()
bar_height[j+1] = key
disp.fill((0, 0, 0))
draw_bars(bar_height)
pygame.time.delay(10)
pygame.display.update()
elif sys.argv[1].lower() == "quicksort":
#quicksort
pygame.display.set_caption("Quick Sort")
def partition(arr, l, r):
# partition function is to bring all the elemets less the pivot to the left and greater than pivot to the right
i = l - 1
pivot = arr[r]
for j in range(l, r):
if(arr[j] < pivot):
i += 1
arr[i], arr[j] = arr[j], arr[i] #swap index i and j
arr[i + 1], arr[r] = arr[r], arr[i + 1]
disp.fill((0, 0, 0))
draw_bars(bar_height)
pygame.time.delay(400)
pygame.display.update()
return i + 1
def partitionRandom(arr, l, r):
if(l < r):
randomIndex = random.randrange(l, r)
arr[r], arr[randomIndex] = arr[randomIndex], arr[r]
return partition(arr, l, r)
def quickSort(arr, l, r):
if r == 0:
return arr
if(l < r):
pi = partitionRandom(arr, l, r) #partion index
quickSort(arr, l, pi - 1)
quickSort(arr, pi + 1, r)
#calling quick sort method
quickSort(bar_height, 0, len(bar_height) - 1)
elif sys.argv[1].lower() == "mergesort":
#mergesort
pygame.display.set_caption("Merge Sort")
def mergeSort(arr):
if len(arr) > 1:
mid = len(arr) // 2
left = arr[:mid]
right = arr[mid:]
mergeSort(left)
mergeSort(right)
i = j = k = 0
while(i < len(left) and j < len(right)):
if(left[i] < right[j]):
arr[k] = left[i]
i += 1
else:
arr[k] = right[j]
j += 1
k += 1
if(i == 0 or j == 0):
disp.fill((0, 0, 0))
draw_bars(bar_height)
pygame.time.delay(10)
pygame.display.update()
else:
disp.fill((0, 0, 0))
draw_bars(bar_height)
pygame.time.delay(100)
pygame.display.update()
while(i < len(left)):
arr[k] = left[i]
i += 1
k += 1
disp.fill((0, 0, 0))
draw_bars(bar_height)
pygame.time.delay(100)
pygame.display.update()
while(j < len(right)):
arr[k] = right[j]
j += 1
k += 1
disp.fill((0, 0, 0))
draw_bars(bar_height)
pygame.time.delay(100)
pygame.display.update()
mergeSort(bar_height)
pygame.quit()