1
1
from tkinter import *
2
2
from tkinter import ttk
3
- from bubble_sort import startBubbleSort
4
3
import random
4
+ sys .path .insert (1 ,'C:\\ Users\\ skili\\ Documents\\ GitHub\\ Sorting-Algorithm-Visualizer\\ algorithms' )
5
+ #importing our specially designed algorithms
6
+ # from linear_search import startLinearSearch
7
+ # from binary_search import startBinarySearch
8
+ from bubble_sort import startBubbleSort
9
+ # from insertion_sort import startInsertionSort
10
+ # from selection_sort import startSelectionSort
11
+ # from merge_sort import startMergeSort
12
+ # from quick_sort import startQuickSort
5
13
14
+ #to store the array/heights of rectangle
6
15
data = []
16
+ #to store the colour of respective rectangles
17
+ colorData = []
18
+
7
19
8
- def visualise (algorithm ,speed ):
20
+
21
+ #to start visualization
22
+ def visualize (algorithm ,stepTime ):
9
23
if algorithm == "Bubble sort" :
10
- startBubbleSort (data ,drawData )
24
+ startBubbleSort (data ,drawData , stepTime )
11
25
12
26
13
27
@@ -31,17 +45,17 @@ def move_window(event):
31
45
32
46
#function to generate random data
33
47
def genData (data_size ):
34
-
35
- global data
36
- data = [ ]
48
+ global data , colorData
49
+ data = colorData = []
50
+ colorData = [ 'grey' for x in range ( int ( float ( data_size ))) ]
37
51
for _ in range (int (float (data_size ))):
38
52
data .append (random .randrange (1 ,100 ))
39
- drawData (data )
53
+ drawData (data , colorData )
40
54
41
55
42
56
43
57
#function to draw rectangles, 'data' is a list of rectangle heights
44
- def drawData (data ):
58
+ def drawData (data , colorData ):
45
59
#clears canvas before drawing new data
46
60
canvas .delete ("all" )
47
61
#setting canvas height,width
@@ -62,7 +76,7 @@ def drawData(data):
62
76
y0 = canvas_h - height * (canvas_h - 20 )
63
77
x1 = (i + 1 )* rectangle_w + (i + 1 )* spacing
64
78
y1 = canvas_h
65
- canvas .create_rectangle (x0 ,y0 ,x1 ,y1 ,fill = "grey" )
79
+ canvas .create_rectangle (x0 ,y0 ,x1 ,y1 ,fill = colorData [ i ] )
66
80
root .update_idletasks ()
67
81
68
82
@@ -90,7 +104,7 @@ def main():
90
104
algorithm_menu = ttk .Combobox (topf ,textvariable = algorithm ,values = ['Bubble sort' ,'Selection Sort' ])
91
105
algorithm_menu .grid (row = 0 ,column = 1 ,padx = 5 ,pady = 5 )
92
106
algorithm_menu .current (0 )
93
- Button (topf ,text = "Visualise" ,bg = 'white' ,command = lambda : visualise (algorithm .get (),speed .get ())).grid (row = 0 ,column = 2 ,padx = 5 ,pady = 5 )
107
+ Button (topf ,text = "Visualise" ,bg = 'white' ,command = lambda : visualize (algorithm .get (),speed .get ())).grid (row = 0 ,column = 2 ,padx = 5 ,pady = 5 )
94
108
95
109
#row 2 of topf
96
110
global size ,speed
@@ -108,7 +122,7 @@ def main():
108
122
109
123
#canvas for visualisation
110
124
global canvas
111
- canvas = Canvas (root ,width = 600 ,height = 380 ,bg = 'black' )
125
+ canvas = Canvas (root ,width = 600 ,height = 380 ,bg = 'black' )
112
126
canvas .grid (row = 1 ,column = 0 ,padx = 10 ,pady = 5 )
113
127
# data=genData(50)
114
128
# drawData(data)
0 commit comments