-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
604 lines (558 loc) · 18.4 KB
/
main.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
from tkinter import messagebox, Label, Button, Entry, PhotoImage, Tk, Menu
from matplotlib import animation
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import numpy as np
rstclicked=False
canvasVisible=True
#list declared with zero elements.
iteration = [0]
loop = 0
index = 0
flag = True
def widgetsDestroy():
global searchLabel, linearButton, binaryButton, sortLabel, bubbleSortButton, selectionSortButton, insertionSortButton, heapSortButton, mergeSortButton, quickSortButton
searchLabel.destroy()
linearButton.destroy()
binaryButton.destroy()
sortLabel.destroy()
bubbleSortButton.destroy()
selectionSortButton.destroy()
insertionSortButton.destroy()
heapSortButton.destroy()
mergeSortButton.destroy()
quickSortButton.destroy()
entry1.destroy()
startButton.destroy()
label1.destroy()
def swap(y, i, j):
if i != j:
y[i], y[j] = y[j], y[i]
def linearSearchAlgo(y,key):
global index
global loop
global flag
flag = True
loop = 0
index = 0
for i in y:
loop+=1
if i == key:
#print(k)
break
index+=1
else:
flag = False
def binarySearchAlgo(y,key):
global index
global loop
global flag
flag = True
loop = 0
index = 0
first = 0
last = len(y) - 1
while (first <= last and flag):
loop+=1
#to get floor number
mid = (first + last) // 2
if y[mid] == key:
index = mid
break
else:
if key < y[mid]:
last = mid - 1
else:
first = mid + 1
if first>last:
flag=False
def bubbleAlgo(y):
if len(y) == 1:
return
swapped = True
for i in range(len(y) - 1):
if not swapped:
break
swapped = False
for j in range(len(y) - 1 - i):
if y[j] > y[j + 1]:
swap(y, j, j + 1)
swapped = True
yield y
def selectionAlgo(y):
if len(y) == 1:
return
for i in range(len(y)):
# Find minimum unsorted value.
minVal = y[i]
minIdx = i
for j in range(i, len(y)):
if y[j] < minVal:
minVal = y[j]
minIdx = j
yield y
swap(y, i, minIdx)
yield y
def insertionAlgo(y):
for i in range(1, len(y)):
j = i
while j > 0 and y[j] < y[j - 1]:
swap(y, j, j - 1)
j -= 1
yield y
def heapify(y,n,i):
largest = i
# leftChild
l = 2 * i + 1
# rightChild
r = 2 * i + 2
if l < n and y[i] < y[l]:
largest = l
if r < n and y[largest] < y[r]:
largest = r
if largest != i:
swap(y,i,largest)
yield y
yield from heapify(y, n, largest)
yield y
def heapAlgo(y):
n = len(y)
#maxheap.
for i in range(n, -1, -1):
yield from heapify(y, n, i)
#deleting Data From maxheap
for i in range(n - 1, 0, -1):
swap(y,i,0)
yield y
yield from heapify(y, i, 0)
def mergesort(y,start,mid,end):
merged = []
leftIdx = start
rightIdx = mid + 1
while leftIdx <= mid and rightIdx <= end:
if y[leftIdx] < y[rightIdx]:
merged.append(y[leftIdx])
leftIdx += 1
else:
merged.append(y[rightIdx])
rightIdx += 1
while leftIdx <= mid:
merged.append(y[leftIdx])
leftIdx += 1
while rightIdx <= end:
merged.append(y[rightIdx])
rightIdx += 1
for i, sorted_val in enumerate(merged):
y[start + i] = sorted_val
yield y
def mergeAlgo(y,start,end):
if end <= start:
return
mid = start + ((end - start + 1) // 2) - 1
yield from mergeAlgo(y, start, mid)
yield from mergeAlgo(y, mid + 1, end)
yield from mergesort(y, start, mid, end)
yield y
def quickAlgo(y,start,end):
if start >= end:
return
pivot = y[end]
pivotIdx = start
for i in range(start, end):
if y[i] < pivot:
swap(y, i, pivotIdx)
pivotIdx += 1
yield y
swap(y, end, pivotIdx)
yield y
yield from quickAlgo(y, start, pivotIdx - 1)
yield from quickAlgo(y, pivotIdx + 1, end)
def visualizeSearching(x,y,title):
def resetSearchClicked():
global canvasVisible
global rstclicked
rstclicked=False
if canvasVisible == True:
canvas.get_tk_widget().destroy()
label3.destroy()
canvasVisible=False
global resetserchButton
global canvasVisible
global label3
canvasVisible = True
width = 0.40
fig = Figure(figsize=(13, 6), dpi=100)
ax = fig.add_subplot(111)
rects = ax.bar(x, y, width, color='#5F9F9F', yerr=None)
#to display the iteration
text = ax.text(0.02, 0.95, "", transform=ax.transAxes)
fig.suptitle(title)
canvas = FigureCanvasTkAgg(fig, root)
text.set_text("No of Operations: {}".format(loop))
if flag:
rects[index].set_color('r')
elif not flag:
label3 = Label(root, text='Number Not Found!!!', font=("Helvetica", 18))
label3.place(x=660, y=10)
canvas.draw()
for rect in rects:
height = rect.get_height()
ax.text(rect.get_x() + rect.get_width() / 2., height + 1,
'%d' % int(height),
ha='center', va='bottom')
canvas.get_tk_widget().pack(expand=1)
resetserchButton = Button(root, text='Reset', command=resetSearchClicked)
resetserchButton.place(x=400, y=10)
def visualize(x, y, generator, title):
def resetClicked():
global canvasVisible
global rstclicked
global label3
rstclicked=False
if canvasVisible == True:
label3.destroy()
canvas.get_tk_widget().destroy()
canvasVisible=False
global canvasVisible
global resetButton
canvasVisible = True
width = 0.40
fig = Figure(figsize=(13, 6), dpi=100)
ax = fig.add_subplot(111)
rects = ax.bar(x, y, width, color='#5F9F9F', yerr=None)
text = ax.text(0.02, 0.95, "", transform=ax.transAxes)
fig.suptitle(title)
canvas = FigureCanvasTkAgg(fig, root) # A tk DrawingArea.
def updateFig(y, rects, iteration):
# zip joins x and y coordinates in pairs
for rect, val in zip(rects, y):
rect.set_height(val)
iteration[0] += 1
text.set_text("No of Operations: {}".format(iteration[0]))
anima = animation.FuncAnimation(fig, func=updateFig,
fargs=(rects, iteration), frames=generator, interval=1,
repeat=False)
canvas.draw()
canvas.get_tk_widget().pack(expand=1)
resetButton = Button(root, text='Reset', command=resetClicked)
resetButton.place(x=400, y=10)
def linearSearchClicked():
widgetsDestroy()
global label
global entry
global label1
global startButton
global entry1
global label3
label3.destroy()
entry1.destroy()
startButton.destroy()
label1.destroy()
root.title("LinearSearch")
label = Label(root, text='Array Size(Max: 50) ')
label.place(x=20,y=10)
entry = Entry(root)
entry.place(x=130,y=10,width=30)
label1 = Label(root, text='Number Tobe Search(Max: 100)')
label1.place(x=180, y=10)
entry1 = Entry(root)
entry1.place(x=355, y=10, width=30)
def startLSrchClicked():
global rstclicked
length = int(entry.get())
key = int(entry1.get())
if length<=50 and key<=100 and rstclicked==False:
# inserting random integers in array with size length from 1 to 100.
y = np.random.randint(1, 101, length)
x = np.arange(1, length + 1, 1)
linearSearchAlgo(y,key)
visualizeSearching(x, y,"LinearSearch\nTime Complexity: O(n)")
rstclicked=True
else:
messagebox.showerror("Error", "Input is Invalid OR press Reset Button.")
startButton = Button(root,text='Start', command=startLSrchClicked)
startButton.place(x=480, y=10)
def binarySearchClicked():
widgetsDestroy()
global label1
global label
global entry
global startButton
global entry1
global label3
label3.destroy()
entry1.destroy()
startButton.destroy()
label1.destroy()
root.title("BinarySearch")
label = Label(root, text='Array Size(Max: 50) ')
label.place(x=20, y=10)
entry = Entry(root)
entry.place(x=130, y=10, width=30)
label1 = Label(root, text='Number Tobe Search(Max: 100)')
label1.place(x=180, y=10)
entry1 = Entry(root)
entry1.place(x=355, y=10, width=30)
def startBSrchClicked():
global rstclicked
length = int(entry.get())
key = int(entry1.get())
if length <= 50 and key <= 100 and rstclicked==False:
y = np.array(np.random.randint(1, 101, length))
y.sort()
x = np.arange(1, length + 1, 1)
binarySearchAlgo(y, key)
visualizeSearching(x, y, "BinarySearch\nTime Complexity: O(log n)")
rstclicked = True
else:
messagebox.showerror("Error", "Input is Invalid OR press Reset Button.")
startButton = Button(root, text='Start', command=startBSrchClicked)
startButton.place(x=480, y=10)
def bubbleSortClicked():
widgetsDestroy()
global label
global entry
global label1
global startButton
global entry1
global label3
label3.destroy()
entry1.destroy()
startButton.destroy()
label1.destroy()
root.title("BubbleSort")
label = Label(root, text='Array Size(Max: 50) ')
label.place(x=20, y=10)
entry = Entry(root)
entry.place(x=130, y=10, width=30)
def startBSClicked():
global rstclicked
global iteration
iteration[0] = 0
length = int(entry.get())
if length <= 50 and rstclicked==False:
y = np.random.randint(1, 101, length)
x = np.arange(1, length + 1, 1)
generator = bubbleAlgo(y)
visualize(x, y, generator, "BubbleSort\nTime Complexity: O(n\u00b2)")
rstclicked = True
else:
messagebox.showerror("Error", "Input is Invalid OR press Reset Button.")
startButton = Button(root, text='Start', command=startBSClicked)
startButton.place(x=480, y=10)
def selectionSortClicked():
widgetsDestroy()
global label
global entry
global label1
global startButton
global entry1
global label3
label3.destroy()
entry1.destroy()
startButton.destroy()
label1.destroy()
root.title("SelectionSort")
label = Label(root, text='Array Size(Max: 50) ')
label.place(x=20, y=10)
entry = Entry(root)
entry.place(x=130, y=10, width=30)
def startSSClicked():
global rstclicked
global iteration
iteration[0] = 0
length = int(entry.get())
if length <= 50 and rstclicked==False:
y = np.random.randint(1, 101, length)
x = np.arange(1, length + 1, 1)
generator = selectionAlgo(y)
visualize(x, y, generator, "SelectionSort\nTime Complexity: O(n\u00b2)")
rstclicked = True
else:
messagebox.showerror("Error", "Input is Invalid OR press Reset Button.")
startButton = Button(root, text='Start', command=startSSClicked)
startButton.place(x=480, y=10)
def insertionSortClicked():
widgetsDestroy()
global label
global entry
global label1
global startButton
global entry1
global label3
label3.destroy()
entry1.destroy()
startButton.destroy()
label1.destroy()
root.title("InsertionSort")
label = Label(root, text='Array Size(Max: 50) ')
label.place(x=20, y=10)
entry = Entry(root)
entry.place(x=130, y=10, width=30)
def startInSClicked():
global rstclicked
global iteration
iteration[0] = 0
length = int(entry.get())
if length <= 50 and rstclicked==False:
y = np.random.randint(1, 101, length)
x = np.arange(1, length + 1, 1)
generator = insertionAlgo(y)
visualize(x, y, generator, "InsertionSort\nTime Complexity: O(n\u00b2)")
rstclicked = True
else:
messagebox.showerror("Error", "Input is Invalid OR press Reset Button.")
startButton = Button(root, text='Start', command=startInSClicked)
startButton.place(x=480, y=10)
def heapSortClicked():
widgetsDestroy()
global label
global entry
global label1
global startButton
global entry1
global label3
label3.destroy()
entry1.destroy()
startButton.destroy()
label1.destroy()
root.title("HeapSort")
label = Label(root, text='Array Size(Max: 50) ')
label.place(x=20, y=10)
entry = Entry(root)
entry.place(x=130, y=10, width=30)
def startHSClicked():
global rstclicked
global iteration
iteration[0] = 0
length = int(entry.get())
if length <= 50 and rstclicked==False:
y = np.random.randint(1, 101, length)
x = np.arange(1, length + 1, 1)
generator = heapAlgo(y)
visualize(x, y, generator, "HeapSort\nTime Complexity: O(nlog(n))")
rstclicked = True
else:
messagebox.showerror("Error", "Input is Invalid OR press Reset Button.")
startButton = Button(root, text='Start', command=startHSClicked)
startButton.place(x=480, y=10)
def mergeSortClicked():
widgetsDestroy()
global label
global entry
global label1
global startButton
global entry1
global label3
label3.destroy()
entry1.destroy()
startButton.destroy()
label1.destroy()
root.title("MergeSort")
label = Label(root, text='Array Size(Max: 50) ')
label.place(x=20, y=10)
entry = Entry(root)
entry.place(x=130, y=10, width=30)
def startMSClicked():
global rstclicked
global iteration
iteration[0] = 0
length = int(entry.get())
if length <= 50 and rstclicked==False:
y = np.random.randint(1, 101, length)
x = np.arange(1, length + 1, 1)
generator = mergeAlgo(y,0,length-1)
visualize(x, y, generator, "MergeSort\nTime Complexity: O(nlog(n))")
rstclicked = True
else:
messagebox.showerror("Error", "Input is Invalid OR press Reset Button.")
startButton = Button(root, text='Start', command=startMSClicked)
startButton.place(x=480, y=10)
def quickSortClicked():
widgetsDestroy()
global label
global entry
global label1
global startButton
global entry1
global label3
label3.destroy()
entry1.destroy()
startButton.destroy()
label1.destroy()
root.title("QuickSort")
label = Label(root, text='Array Size(Max: 50) ')
label.place(x=20, y=10)
entry = Entry(root)
entry.place(x=130, y=10, width=30)
def startQSClicked():
global rstclicked
global iteration
iteration[0] = 0
length = int(entry.get())
if length <= 50 and rstclicked==False:
y = np.random.randint(1, 101, length)
x = np.arange(1, length + 1, 1)
generator = quickAlgo(y,0,length-1)
visualize(x, y, generator, "QuickSort\nTime Complexity: O(n\u00b2)")
rstclicked = True
else:
messagebox.showerror("Error", "Input is Invalid OR press Reset Button.")
startButton = Button(root, text='Start', command=startQSClicked)
startButton.place(x=480, y=10)
root = Tk()
root.title("AlgoPlay")
root.state('zoomed')
root.iconbitmap('images/logo.ico')
linearPhoto = PhotoImage(file="images/Linear_shadow.png")
binaryPhoto = PhotoImage(file="images/Binary_shadow.png")
bubblePhoto = PhotoImage(file="images/Bubble_shadow.png")
selectionPhoto = PhotoImage(file="images/Selection_shadow.png")
insertionPhoto = PhotoImage(file="images/Insertion_shadow.png")
heapPhoto = PhotoImage(file="images/Heap_shadow.png")
mergePhoto = PhotoImage(file="images/Merge_shadow.png")
quickPhoto = PhotoImage(file="images/Quick_shadow.png")
#global objects
entry=Entry(root)
entry1 = Entry(root)
startButton = Button(root, text='')
label=Label(root, text='')
label1 = Label(root, text='')
label3 = Label(root, text='')
searchLabel = Label(root, text='Searching Algorithm', font = ('Comic Sans MS',20),fg = 'black')
searchLabel.place(x=20, y=10)
linearButton = Button(root, image=linearPhoto, command=linearSearchClicked, border=0,height=190,width=341)
linearButton.place(x=20, y=50)
binaryButton = Button(root, image=binaryPhoto,command=binarySearchClicked, border=0,height=190,width=341)
binaryButton.place(x=400, y=50)
sortLabel = Label(root, text='Sorting Algorithm', font = ('Comic Sans MS',20),fg = 'black')
sortLabel.place(x=20, y=260)
bubbleSortButton = Button(root, image=bubblePhoto, command=bubbleSortClicked, border=0,height=190,width=341)
bubbleSortButton.place(x=20, y=300)
selectionSortButton = Button(root, image=selectionPhoto, command=selectionSortClicked, border=0,height=190,width=341)
selectionSortButton.place(x=400, y=300)
insertionSortButton = Button(root, image=insertionPhoto, command=insertionSortClicked, border=0,height=190,width=341)
insertionSortButton.place(x=780, y=300)
heapSortButton = Button(root, image=heapPhoto, command=heapSortClicked, border=0,height=190,width=341)
heapSortButton.place(x=20, y=500)
mergeSortButton = Button(root, image=mergePhoto, command=mergeSortClicked, border=0,height=190,width=341)
mergeSortButton.place(x=400, y=500)
quickSortButton = Button(root, image=quickPhoto, command=quickSortClicked, border=0,height=190,width=341)
quickSortButton.place(x=780, y=500)
menu = Menu(root)
root.config(menu=menu)
Searchmenu = Menu(menu)
Searchmenu = Menu(menu, tearoff=0)
menu.add_cascade(label='Searching', menu=Searchmenu)
Searchmenu.add_command(label='Linear Search', command=linearSearchClicked)
Searchmenu.add_command(label='Binary Search', command=binarySearchClicked)
Sortmenu = Menu(menu)
Sortmenu = Menu(menu, tearoff=0)
menu.add_cascade(label='Sorting', menu=Sortmenu)
Sortmenu.add_command(label='BubbleSort', command=bubbleSortClicked)
Sortmenu.add_command(label='SelectionSort', command=selectionSortClicked)
Sortmenu.add_command(label='InsertionSort', command=insertionSortClicked)
Sortmenu.add_command(label='HeapSort', command=heapSortClicked)
Sortmenu.add_command(label='MergeSort', command=mergeSortClicked)
Sortmenu.add_command(label='QuickSort', command=quickSortClicked)
root.mainloop()