-
Notifications
You must be signed in to change notification settings - Fork 4
/
sortingnew.py
221 lines (162 loc) · 5.35 KB
/
sortingnew.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
#Import the required Libraries
from tkinter import *
import tkinter as tk
import random
import time
#accepting value:
#n =int(input("Enter Total Number of Elements: "))
#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
time.sleep(0.5)
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 = 5
barend = 15
barList = []
lengthList = []
#Creating a rectangle
for bar in range(0, (number)):
randomY = random.randint(1, 360)
bar = canvas.create_rectangle(barstart, randomY, barend, 365, fill='yellow')
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='black')
#for Accepting total number of Inputs
def Accept_value():
global number
t1=int(a.get())
number = t1
#Main Code(Driver Code)
root = Tk()
# specify size of window.
root.geometry("700x350")
# Create text widget and specify size.
T = Text(root, height = 5, width = 52,bg = "yellow")
# Create label
l = Label(root, text = "SORTING ALGORITHM VISUALIZER")
l.config(font =("Courier", 14))
Fact = """MINI PROJECT
Group Members are :-
ADITYA SURYAWANSHI
MIHIR SONAWANE"""
# Create an Exit button.
b2 = Button(root, text = "NEXT",
command = root.destroy)
l.pack()
T.pack()
b2.pack()
# Insert The Fact.
T.insert(tk.END, Fact)
root.mainloop()
#ACCEPT NUMBER OF Input
win = tk.Tk()
win.geometry("700x350")
# Create an Entry widget
a=Entry(win, width=35)
a.pack()
Button(win, text="Enter number of Display BARS from", command=Accept_value).pack()
Button(win, text="Next", command=win.destroy).pack()
win.mainloop()
#Making a window using the Tk widget
window = tk.Tk()
window.title('Sorting Visualizer')
window.geometry('1000x450')
#Making a Canvas within the window to display contents
canvas = tk.Canvas(window, width='1000', 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()