1313dataset = []
1414stop_flag = False
1515sorting_algorithm = StringVar ()
16+ graph_type = StringVar ()
17+ sorting_alg = SortingAlgorithms ()
1618
1719
1820# Buttons
@@ -27,31 +29,26 @@ def start_btn():
2729 if stop_flag :
2830 stop_flag = False
2931 if sorting_algorithm .get () == 'Bubble Sort' :
30- SortingAlgorithms .bubble_sort (dataset , draw_data , animation_speed .get (), stop_flag )
31- comparisons_count = SortingAlgorithms .bubble_sort (dataset , draw_data , animation_speed .get (), stop_flag )
32- update_header_labels (comparisons_count , 'O(n^2)' )
32+ sorting_alg .bubble_sort (dataset , draw_data , animation_speed .get (), stop_flag )
33+ update_header_labels (sorting_alg .get_comparisons_count (), 'O(n^2)' )
3334
3435 elif sorting_algorithm .get () == 'Quick Sort' :
35- SortingAlgorithms .quick_sort (dataset , 0 , len (dataset ) - 1 , draw_data , animation_speed .get ())
36+ sorting_alg .quick_sort (dataset , 0 , len (dataset ) - 1 , draw_data , animation_speed .get ())
3637 draw_data (dataset , ['green' for i in range (len (dataset ))])
37- comparisons_count = SortingAlgorithms .quick_sort (dataset , 0 , len (dataset ) - 1 , draw_data , animation_speed .get ())
38- update_header_labels (comparisons_count , 'O(n log n)' )
38+ update_header_labels (sorting_alg .get_comparisons_count (), 'O(n log n)' )
3939
4040 elif sorting_algorithm .get () == 'Insertion Sort' :
41- SortingAlgorithms .insertion_sort (dataset , draw_data , animation_speed .get ())
42- comparisons_count = SortingAlgorithms .insertion_sort (dataset , draw_data , animation_speed .get ())
43- update_header_labels (comparisons_count , 'O(n^2)' )
41+ sorting_alg .insertion_sort (dataset , draw_data , animation_speed .get ())
42+ update_header_labels (sorting_alg .get_comparisons_count (), 'O(n^2)' )
4443
4544 elif sorting_algorithm .get () == 'Selection Sort' :
46- SortingAlgorithms .selection_sort (dataset , draw_data , animation_speed .get ())
47- comparisons_count = SortingAlgorithms .selection_sort (dataset , draw_data , animation_speed .get ())
48- update_header_labels (comparisons_count , 'O(n^2)' )
45+ sorting_alg .selection_sort (dataset , draw_data , animation_speed .get ())
46+ update_header_labels (sorting_alg .get_comparisons_count (), 'O(n^2)' )
4947
5048 elif sorting_algorithm .get () == 'Merge Sort' :
51- SortingAlgorithms .merge_sort (dataset , draw_data , animation_speed .get ())
49+ sorting_alg .merge_sort (dataset , draw_data , animation_speed .get ())
5250 draw_data (dataset , ['green' for i in range (len (dataset ))])
53- # comparisons_count = SortingAlgorithms.merge_sort(dataset, draw_data, animation_speed.get())
54- update_header_labels (10 , 'O(n^2)' )
51+ update_header_labels (sorting_alg .get_comparisons_count (), 'O(n^2)' )
5552
5653 generateBtn .config (state = NORMAL )
5754 startBtn .config (state = NORMAL )
@@ -72,28 +69,66 @@ def reset_btn():
7269 draw_data (dataset , [])
7370
7471
75- # Data manipulation
72+ # A utility function to draw the dataset. It can draw the dataset in 3 different ways:
73+ # 1. Bar
74+ # 2. Scatter
75+ # 3. Stem
7676def draw_data (data_set , clr ):
77- cv .delete ('all' )
78- cv_height = 380
79- cv_width = 700
80- x_width = cv_width / (len (data_set ) + 1 )
81- offset = 30
82- space_between = 5
83- data = [i / max (data_set ) for i in data_set ]
84- 85- for i , h in enumerate (data ):
86- x0 = i * x_width + offset + space_between
87- y0 = cv_height - h * 320
88- x1 = (i + 1 ) * x_width + offset
89- y1 = cv_height
90- 91- cv .create_rectangle (x0 , y0 , x1 , y1 , fill = clr [i ])
92- cv .create_text (x0 + 2 , y0 , anchor = SW , text = str (data_set [i ]))
77+ 78+ if graph_type .get () == 'Bar' :
79+ cv .delete ('all' )
80+ cv_height = 380
81+ cv_width = 700
82+ x_width = cv_width / (len (data_set ) + 1 )
83+ offset = 30
84+ space_between = 5
85+ data = [i / max (data_set ) for i in data_set ]
86+ 87+ for i , h in enumerate (data ):
88+ x0 = i * x_width + offset + space_between
89+ y0 = cv_height - h * 320
90+ x1 = (i + 1 ) * x_width + offset
91+ y1 = cv_height
92+ 93+ cv .create_rectangle (x0 , y0 , x1 , y1 , fill = clr [i ])
94+ cv .create_text (x0 + 2 , y0 , anchor = SW , text = str (data_set [i ]))
95+ 96+ elif graph_type .get () == 'Stem' :
97+ cv .delete ('all' )
98+ cv_height = 380
99+ cv_width = 700
100+ x_width = cv_width / (len (data_set ) + 1 )
101+ offset = 30
102+ data = [i / max (data_set ) for i in data_set ]
103+ 104+ for i , h in enumerate (data ):
105+ x = i * x_width + offset
106+ y = cv_height - h * 320
107+ 108+ cv .create_line (x , cv_height , x , y , fill = clr [i ], width = 2 )
109+ cv .create_oval (x - 2 , y - 2 , x + 2 , y + 2 , fill = clr [i ])
110+ cv .create_text (x + 5 , y - 5 , anchor = SW , text = str (data_set [i ]))
111+ 112+ elif graph_type .get () == 'Scatter' :
113+ cv .delete ('all' )
114+ cv_height = 380
115+ cv_width = 700
116+ x_width = cv_width / (len (data_set ) + 1 )
117+ offset = 30
118+ data = [i / max (data_set ) for i in data_set ]
119+ 120+ for i , h in enumerate (data ):
121+ x = i * x_width + offset
122+ y = cv_height - h * 320
123+ 124+ cv .create_oval (x - 2 , y - 2 , x + 2 , y + 2 , fill = clr [i ])
125+ cv .create_text (x + 5 , y - 5 , anchor = SW , text = str (data_set [i ]))
93126
94127 root .update_idletasks ()
95128
96129
130+ # A utility function to generate a dataset with random numbers
131+ # this function is called in case the user did not enter any values through entry
97132def generate_dataset ():
98133 global dataset
99134 min_val = int (dataset_min_value .get ())
@@ -107,6 +142,8 @@ def generate_dataset():
107142 draw_data (dataset , ['#FF597B' for i in range (len (dataset ))])
108143
109144
145+ # A utility function to show the comparisons count and time
146+ # complexity of a sorting algorithm to user
110147def update_header_labels (comparisons_count , time_complexity ):
111148 comparison_label .config (text = f"Comparisons Count: { comparisons_count } " )
112149 algorithm_complexity_label .config (text = f"Algorithm Complexity: { time_complexity } " )
@@ -118,10 +155,13 @@ def process_input():
118155 # Process the input array
119156 array = [int (x ) for x in user_input .split ("," )]
120157 dataset = []
158+ # copying the entry values into our dataset
121159 for i in array :
122160 dataset .append (i )
123161 # draw data
124162 draw_data (dataset , ['#FF597B' for i in range (len (dataset ))])
163+ # make the entry empty after its processed
164+ entry .delete (0 , END )
125165
126166
127167# GUI Setup
@@ -140,38 +180,41 @@ def process_input():
140180cv = Canvas (root , width = 820 , height = 480 , background = '#fff' )
141181cv .grid (row = 1 , column = 1 , padx = 0 , pady = 5 , columnspan = 1 )
142182
143- combobox = tkinter .ttk .Combobox (sidebar_fr , values = ['Bubble Sort' , 'Quick Sort' , 'Insertion Sort' , 'Selection Sort' , 'Merge Sort' ], textvariable = sorting_algorithm )
144- combobox .grid (row = 1 , column = 0 , padx = 5 , pady = 5 )
145- combobox .current (0 )
183+ sorting_algo = tkinter .ttk .Combobox (sidebar_fr , values = ['Sorting Algorithm' , 'Bubble Sort' , 'Quick Sort' , 'Insertion Sort' , 'Selection Sort' , 'Merge Sort' ], textvariable = sorting_algorithm )
184+ sorting_algo .grid (row = 1 , column = 0 , padx = 5 , pady = 5 )
185+ sorting_algo .current (0 )
146186
187+ graph = tkinter .ttk .Combobox (sidebar_fr , values = ['Graph Type' , 'Bar' , 'Scatter' , 'Stem' ], textvariable = graph_type )
188+ graph .grid (row = 2 , column = 0 , padx = 5 , pady = 5 )
189+ graph .current (0 )
147190
148191dataset_size = Scale (sidebar_fr , from_ = 3 , to = 50 , resolution = 1 , orient = HORIZONTAL , label = "Dataset Size" , background = '#fff' , length = 150 )
149- dataset_size .grid (row = 2 , column = 0 , padx = 5 , pady = 5 , sticky = W )
192+ dataset_size .grid (row = 3 , column = 0 , padx = 5 , pady = 5 , sticky = W )
150193
151194dataset_min_value = Scale (sidebar_fr , from_ = 1 , to = 100 , resolution = 1 , orient = HORIZONTAL , label = "Minimum Value" , background = '#fff' , length = 150 )
152- dataset_min_value .grid (row = 3 , column = 0 , padx = 5 , pady = 5 , sticky = W )
195+ dataset_min_value .grid (row = 4 , column = 0 , padx = 5 , pady = 5 , sticky = W )
153196
154197dataset_max_value = Scale (sidebar_fr , from_ = 100 , to = 1000 , resolution = 1 , orient = HORIZONTAL , label = "Maximum Value" , background = '#fff' , length = 150 )
155- dataset_max_value .grid (row = 4 , column = 0 , padx = 5 , pady = 5 , sticky = W )
198+ dataset_max_value .grid (row = 5 , column = 0 , padx = 5 , pady = 5 , sticky = W )
156199
157200animation_speed = Scale (sidebar_fr , from_ = 0.1 , to = 5.0 , length = 150 , digits = 2 , resolution = 0.1 , orient = HORIZONTAL , label = 'Select Speed(sec)' , background = '#fff' )
158- animation_speed .grid (row = 5 , column = 0 , padx = 5 , pady = 5 , sticky = W )
201+ animation_speed .grid (row = 6 , column = 0 , padx = 5 , pady = 5 , sticky = W )
159202
160203generateBtn = Button (sidebar_fr , text = 'Generate Dataset' , command = generate_dataset , bg = '#764AF1' , fg = 'white' , width = 20 )
161- generateBtn .grid (row = 6 , column = 0 , padx = 5 , pady = 5 )
204+ generateBtn .grid (row = 7 , column = 0 , padx = 5 , pady = 5 )
162205startBtn = Button (sidebar_fr , text = 'Start' , command = start_btn , bg = '#019267' , fg = 'white' , height = 1 , width = 20 )
163- startBtn .grid (row = 7 , column = 0 , padx = 5 , pady = 5 )
206+ startBtn .grid (row = 8 , column = 0 , padx = 5 , pady = 5 )
164207resetBtn = Button (sidebar_fr , text = 'Reset' , command = reset_btn , bg = '#FF597B' , fg = 'white' , height = 1 , width = 20 )
165- resetBtn .grid (row = 8 , column = 0 , padx = 5 , pady = 5 )
208+ resetBtn .grid (row = 9 , column = 0 , padx = 5 , pady = 5 )
166209stopBtn = Button (sidebar_fr , text = 'Stop' , command = stop_btn , bg = 'orange' , fg = 'white' , height = 1 , width = 20 , state = DISABLED )
167- stopBtn .grid (row = 9 , column = 0 , padx = 5 , pady = 5 )
210+ stopBtn .grid (row = 10 , column = 0 , padx = 5 , pady = 5 )
168211
169212
170- Label (sidebar_fr , text = "---------Or---------" , bg = '#ECF2FF' , font = ('consolas' , 10 , "bold" )).grid (row = 10 , column = 0 , padx = 5 , pady = 5 )
171- Label (sidebar_fr , text = "Enter numbers(,)" , bg = '#ECF2FF' , font = ('consolas' , 10 , "bold" )).grid (row = 11 , column = 0 , padx = 5 , pady = 5 )
213+ Label (sidebar_fr , text = "---------Or---------" , bg = '#ECF2FF' , font = ('consolas' , 10 , "bold" )).grid (row = 11 , column = 0 , padx = 5 , pady = 5 )
214+ Label (sidebar_fr , text = "Enter numbers(,)" , bg = '#ECF2FF' , font = ('consolas' , 10 , "bold" )).grid (row = 12 , column = 0 , padx = 5 , pady = 5 )
172215entry = Entry (sidebar_fr , width = 25 )
173- entry .grid (row = 12 , column = 0 , padx = 5 , pady = 5 )
174- Button (sidebar_fr , text = "Process Input" , height = 1 , width = 20 , fg = '#fff' , bg = 'purple' , command = process_input ).grid (row = 13 , column = 0 , padx = 5 , pady = 5 )
216+ entry .grid (row = 13 , column = 0 , padx = 5 , pady = 5 )
217+ Button (sidebar_fr , text = "Process Input" , height = 1 , width = 20 , fg = '#fff' , bg = 'purple' , command = process_input ).grid (row = 14 , column = 0 , padx = 5 , pady = 5 )
175218
176219
177220# start the main event loop of the Application
0 commit comments