-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortingfinal.py
More file actions
221 lines (197 loc) · 5.05 KB
/
Copy pathsortingfinal.py
File metadata and controls
221 lines (197 loc) · 5.05 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
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 turtle
import random
import time
# Screen
wn = turtle.Screen()
wn.title("Sorting Visualiser")
wn.setup(width=1200, height=700)
wn.tracer(0)
# Background
def display_vis():
dispbg = turtle.Turtle()
dispbg.color("grey")
dispbg.fillcolor("#DAECFF")
dispbg.speed(0)
dispbg.hideturtle()
dispbg.penup()
dispbg.width(4)
dispbg.goto(-412.5, 266)
dispbg.pendown()
dispbg.begin_fill()
for i in range(2):
dispbg.forward(831.5)
dispbg.right(90)
dispbg.forward(569)
dispbg.right(90)
dispbg.end_fill()
wn.update()
# Initialization
a = []
l = []
d = 8
pen = [0] * 4
writer = [0] * 4
write_time = turtle.Turtle()
write_time.hideturtle()
# Hiding Turtle
def hide_turtle():
for i in range(4):
pen[i].reset()
pen[i].hideturtle()
writer[i].reset()
writer[i].hideturtle()
wn.update()
# Showing Turtle
def show_turtle():
function_button()
# Generating Lines
def generate_lines():
global d, a, l
for _ in range(100):
line = turtle.Turtle()
line.hideturtle()
line.shape("square")
line.color("cyan")
alen = random.uniform(0.1, 28)
line.shapesize(stretch_len=alen, stretch_wid=0.2)
l.append(alen)
line.speed(0)
line.left(90)
line.penup()
res_len_sel = 300 - (alen * 10)
line.goto((-400 + d), -res_len_sel)
line.showturtle()
a.append(line)
d += 8
d = 8
wn.update()
# Selection Sort
def sel_sort():
for i in range(len(l)):
a[i].color("#FF1100")
var = a[i].xcor()
length = l[i]
pos = i
for j in range(i + 1, 100):
if l[j] < length:
length = l[j]
var = a[j].xcor()
pos = j
a[j].color("cyan")
a[pos].color("green")
a[pos].setx(a[i].xcor())
a[i].setx(var)
a[i], a[pos] = a[pos], a[i]
l[i], l[pos] = l[pos], l[i]
wn.update()
# Bubble Sort
def bub_sort():
for i in range(len(l) - 1):
for j in range(len(l) - 1 - i):
var = a[j].xcor()
vara = a[j + 1].xcor()
if l[j] > l[j + 1]:
a[j].setx(vara)
a[j + 1].setx(var)
a[j], a[j + 1] = a[j + 1], a[j]
l[j], l[j + 1] = l[j + 1], l[j]
a[j].color("cyan")
a[j + 1].color("green")
wn.update()
a[0].color("green")
wn.update()
# Insertion Sort
def ins_sort():
for i in range(len(l)):
j = i
a[j].color("red")
while j > 0 and l[j - 1] > l[j]:
xcor = a[j - 1].xcor()
a[j - 1].setx(a[j].xcor())
a[j].setx(xcor)
l[j - 1], l[j] = l[j], l[j - 1]
a[j - 1], a[j] = a[j], a[j - 1]
j -= 1
wn.update()
a[j].color("Green")
# Clearing Screen
def clear_screen():
global a, l
for line in a:
line.clear()
line.hideturtle()
del a[:]
del l[:]
generate_lines()
# Generating Buttons
def generate_buttons(x, y, obj, msg):
pen[obj] = turtle.Turtle()
pen[obj].hideturtle()
pen[obj].penup()
pen[obj].goto(x - 80, y - 20)
pen[obj].pendown()
pen[obj].color("#52A1FA")
pen[obj].begin_fill()
for i in range(2):
pen[obj].fd(160)
pen[obj].left(90)
pen[obj].fd(40)
pen[obj].left(90)
pen[obj].end_fill()
writer[obj] = turtle.Turtle()
writer[obj].hideturtle()
writer[obj].penup()
writer[obj].goto(x, y - 7.5)
writer[obj].write(msg, font=('Verdana', 12, 'normal'), align="center")
wn.update()
# Generating Buttons
def function_button():
generate_buttons(-300, 300, 0, "Create New Lines")
generate_buttons(-100, 300, 1, "Selection Sort")
generate_buttons(100, 300, 2, "Bubble Sort")
generate_buttons(300, 300, 3, "Insertion Sort")
# Time Taken
def write_timeTaken(t):
global write_time
write_time.hideturtle()
write_time.penup()
write_time.goto(0, -325)
write_time.write("Time Taken: " + str(t), font=('Verdana', 12, 'normal'), align="center")
wn.update()
# Checking Screen Click
def checki(x, y):
write_time.clear()
if x > (-380) and x < (-220) and y > 280 and y < 320:
hide_turtle()
clear_screen()
show_turtle()
elif x > (-180) and x < (-20) and y > 280 and y < 320:
hide_turtle()
t1 = time.time()
sel_sort()
t2 = time.time()
write_timeTaken(t2 - t1)
show_turtle()
elif x > 20 and x < 180 and y > 280 and y < 320:
hide_turtle()
t1 = time.time()
bub_sort()
t2 = time.time()
write_timeTaken(t2 - t1)
show_turtle()
elif x > 220 and x < 380 and y > 280 and y < 320:
hide_turtle()
t1 = time.time()
ins_sort()
t2 = time.time()
write_timeTaken(t2 - t1)
show_turtle()
# Screen
display_vis()
# Creating Lines
generate_lines()
# Generating Buttons
function_button()
turtle.onscreenclick(checki)
turtle.listen()
turtle.done()