-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_visual.py
More file actions
155 lines (120 loc) · 4.01 KB
/
Copy pathsort_visual.py
File metadata and controls
155 lines (120 loc) · 4.01 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
import tkinter as tk
import random
#Function to swap two bars that will be animated
def swap(pos_0, pos_1):
bar11, _, bar12, _ = canvas.coords(pos_0)
bar21, _, bar22, _ = canvas.coords(pos_1)
canvas.move(pos_0, bar21-bar11, 0)
canvas.move(pos_1, bar12-bar22, 0)
worker = None
#Insertion Sort
def _insertion_sort():
global barList
global lengthList
for i in range(len(lengthList)):
cursor = lengthList[i]
cursorBar = barList[i]
pos = i
while pos > 0 and lengthList[pos - 1] > cursor:
lengthList[pos] = lengthList[pos - 1]
barList[pos], barList[pos - 1] = barList[pos - 1], barList[pos]
swap(barList[pos],barList[pos-1])
yield
pos -= 1
lengthList[pos] = cursor
barList[pos] = cursorBar
swap(barList[pos],cursorBar)
#Bubble Sort
def _bubble_sort():
global barList
global lengthList
for i in range(len(lengthList) - 1):
for j in range(len(lengthList) - i - 1):
if(lengthList[j] > lengthList[j + 1]):
lengthList[j] , lengthList[j + 1] = lengthList[j + 1] , lengthList[j]
barList[j], barList[j + 1] = barList[j + 1] , barList[j]
swap(barList[j + 1] , barList[j])
yield
#Selection Sort
def _selection_sort():
global barList
global lengthList
for i in range(len(lengthList)):
min = i
for j in range(i + 1 ,len(lengthList)):
if(lengthList[j] < lengthList[min]):
min = j
lengthList[min], lengthList[i] = lengthList[i] ,lengthList[min]
barList[min] , barList[i] = barList[i] , barList[min]
swap(barList[min] , barList[i])
yield
#Triggering Fuctions
def insertion_sort():
global worker
worker = _insertion_sort()
animate()
def selection_sort():
global worker
worker = _selection_sort()
animate()
def bubble_sort():
global worker
worker = _bubble_sort()
animate()
#Animation Function
def animate():
global worker
if worker is not None:
try:
next(worker)
window.after(10, animate)
except StopIteration:
worker = None
finally:
window.after_cancel(animate)
#Generator function for generating data
def generate():
global barList
global lengthList
canvas.delete('all')
barstart = 1
barend = 8
barList = []
lengthList = []
#Creating a rectangle
for bar in range(1, 60):
randomY = random.randint(1, 360)
bar = canvas.create_rectangle(barstart, randomY, barend, 365, fill='blue')
barList.append(bar)
barstart += 10
barend += 10
#Getting length of the bar and appending into length list
for bar in barList:
bar = canvas.coords(bar)
length = bar[3] - bar[1]
lengthList.append(length)
#Maximum is colored Red
#Minimum is colored Black
for i in range(len(lengthList)-1):
if lengthList[i] == min(lengthList):
canvas.itemconfig(barList[i], fill='red')
elif lengthList[i] == max(lengthList):
canvas.itemconfig(barList[i], fill='green')
#Making a window using the Tk widget
window = tk.Tk()
window.title('Sorting Visualizer')
window.geometry('600x450')
#Making a Canvas within the window to display contents
canvas = tk.Canvas(window, width='600', height='400')
canvas.grid(column=0,row=0, columnspan = 50)
#Buttons
insert = tk.Button(window, text='Insertion Sort', command=insertion_sort)
select = tk.Button(window, text='Selection Sort', command=selection_sort)
bubble = tk.Button(window, text='Bubble Sort', command=bubble_sort)
shuf = tk.Button(window, text='Shuffle', command=generate)
insert.grid(column=1,row=1)
select.grid(column=2,row=1)
bubble.grid(column=3,row=1)
shuf.grid(column=0, row=1)
generate()
window.mainloop()