Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1a25e2d

Browse files
Add files via upload
1 parent 624b668 commit 1a25e2d

File tree

1 file changed

+221
-0
lines changed

1 file changed

+221
-0
lines changed

‎sortingnew.py

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
#Import the required Libraries
2+
from tkinter import *
3+
import tkinter as tk
4+
import random
5+
import time
6+
7+
8+
9+
#accepting value:
10+
11+
#n =int(input("Enter Total Number of Elements: "))
12+
13+
#Function to swap two bars that will be animated
14+
def swap(pos_0, pos_1):
15+
16+
bar11, _, bar12, _ = canvas.coords(pos_0)
17+
bar21, _, bar22, _ = canvas.coords(pos_1)
18+
canvas.move(pos_0, bar21-bar11, 0)
19+
canvas.move(pos_1, bar12-bar22, 0)
20+
21+
22+
worker = None
23+
24+
#Insertion Sort
25+
def _insertion_sort():
26+
global barList
27+
global lengthList
28+
29+
for i in range(len(lengthList)):
30+
cursor = lengthList[i]
31+
cursorBar = barList[i]
32+
pos = i
33+
34+
while pos > 0 and lengthList[pos - 1] > cursor:
35+
lengthList[pos] = lengthList[pos - 1]
36+
barList[pos], barList[pos - 1] = barList[pos - 1], barList[pos]
37+
swap(barList[pos],barList[pos-1])
38+
yield
39+
pos -= 1
40+
41+
lengthList[pos] = cursor
42+
barList[pos] = cursorBar
43+
swap(barList[pos],cursorBar)
44+
45+
46+
#Bubble Sort
47+
def _bubble_sort():
48+
global barList
49+
global lengthList
50+
51+
for i in range(len(lengthList) - 1):
52+
for j in range(len(lengthList) - i - 1):
53+
if(lengthList[j] > lengthList[j + 1]):
54+
lengthList[j] , lengthList[j + 1] = lengthList[j + 1] , lengthList[j]
55+
barList[j], barList[j + 1] = barList[j + 1] , barList[j]
56+
swap(barList[j + 1] , barList[j])
57+
yield
58+
59+
60+
#Selection Sort
61+
def _selection_sort():
62+
global barList
63+
global lengthList
64+
65+
for i in range(len(lengthList)):
66+
min = i
67+
time.sleep(0.5)
68+
for j in range(i + 1 ,len(lengthList)):
69+
if(lengthList[j] < lengthList[min]):
70+
min = j
71+
lengthList[min], lengthList[i] = lengthList[i] ,lengthList[min]
72+
barList[min] , barList[i] = barList[i] , barList[min]
73+
74+
swap(barList[min] , barList[i])
75+
76+
yield
77+
78+
79+
#Triggering Fuctions
80+
81+
def insertion_sort():
82+
global worker
83+
worker = _insertion_sort()
84+
animate()
85+
86+
def selection_sort():
87+
global worker
88+
worker = _selection_sort()
89+
animate()
90+
91+
def bubble_sort():
92+
global worker
93+
worker = _bubble_sort()
94+
animate()
95+
96+
97+
98+
#Animation Function
99+
def animate():
100+
global worker
101+
if worker is not None:
102+
try:
103+
next(worker)
104+
window.after(10, animate)
105+
except StopIteration:
106+
worker = None
107+
finally:
108+
window.after_cancel(animate)
109+
110+
111+
#Generator function for generating data
112+
def generate():
113+
global barList
114+
global lengthList
115+
canvas.delete('all')
116+
barstart = 5
117+
barend = 15
118+
barList = []
119+
lengthList = []
120+
121+
#Creating a rectangle
122+
for bar in range(0, (number)):
123+
randomY = random.randint(1, 360)
124+
bar = canvas.create_rectangle(barstart, randomY, barend, 365, fill='yellow')
125+
barList.append(bar)
126+
barstart += 10
127+
barend += 10
128+
129+
#Getting length of the bar and appending into length list
130+
for bar in barList:
131+
bar = canvas.coords(bar)
132+
length = bar[3] - bar[1]
133+
lengthList.append(length)
134+
135+
#Maximum is colored Red
136+
#Minimum is colored Black
137+
for i in range(len(lengthList)-1):
138+
if lengthList[i] == min(lengthList):
139+
canvas.itemconfig(barList[i], fill='red')
140+
elif lengthList[i] == max(lengthList):
141+
canvas.itemconfig(barList[i], fill='black')
142+
143+
144+
#for Accepting total number of Inputs
145+
def Accept_value():
146+
global number
147+
t1=int(a.get())
148+
number = t1
149+
150+
151+
152+
#Main Code(Driver Code)
153+
154+
155+
root = Tk()
156+
# specify size of window.
157+
root.geometry("700x350")
158+
# Create text widget and specify size.
159+
T = Text(root, height = 5, width = 52,bg = "yellow")
160+
# Create label
161+
l = Label(root, text = "SORTING ALGORITHM VISUALIZER")
162+
l.config(font =("Courier", 14))
163+
164+
Fact = """MINI PROJECT
165+
Group Members are :-
166+
ADITYA SURYAWANSHI
167+
MIHIR SONAWANE"""
168+
169+
170+
# Create an Exit button.
171+
b2 = Button(root, text = "NEXT",
172+
command = root.destroy)
173+
174+
l.pack()
175+
T.pack()
176+
b2.pack()
177+
178+
# Insert The Fact.
179+
T.insert(tk.END, Fact)
180+
root.mainloop()
181+
182+
#ACCEPT NUMBER OF Input
183+
win = tk.Tk()
184+
win.geometry("700x350")
185+
# Create an Entry widget
186+
a=Entry(win, width=35)
187+
a.pack()
188+
Button(win, text="Enter number of Display BARS from", command=Accept_value).pack()
189+
Button(win, text="Next", command=win.destroy).pack()
190+
191+
win.mainloop()
192+
193+
194+
195+
196+
#Making a window using the Tk widget
197+
window = tk.Tk()
198+
window.title('Sorting Visualizer')
199+
window.geometry('1000x450')
200+
201+
202+
#Making a Canvas within the window to display contents
203+
canvas = tk.Canvas(window, width='1000', height='400')
204+
canvas.grid(column=0,row=0, columnspan = 50)
205+
206+
#Buttons
207+
insert = tk.Button(window, text='Insertion Sort', command=insertion_sort)
208+
select = tk.Button(window, text='Selection Sort', command=selection_sort)
209+
bubble = tk.Button(window, text='Bubble Sort', command=bubble_sort)
210+
shuf = tk.Button(window, text='Shuffle', command=generate)
211+
insert.grid(column=1,row=1)
212+
select.grid(column=2,row=1)
213+
bubble.grid(column=3,row=1)
214+
shuf.grid(column=0, row=1)
215+
216+
generate()
217+
window.mainloop()
218+
219+
220+
221+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /