@@ -17,7 +17,6 @@ def selection(A):
17
17
if A [j ] < A [position ]:
18
18
position = j
19
19
A [i ], A [position ] = A [position ], A [i ]
20
-
21
20
22
21
###############################################################
23
22
@@ -57,7 +56,6 @@ def bubble(A):
57
56
if A [i ] > A [i + 1 ]:
58
57
A [i ], A [i + 1 ] = A [i + 1 ], A [i ]
59
58
60
-
61
59
###############################################################
62
60
63
61
def shell (A ):
@@ -158,6 +156,15 @@ def partition(A, low, high):
158
156
return j
159
157
160
158
def quicksort (A , low , high ):
159
+ """
160
+ Algo name: Quick Sort
161
+ UNSTABLE
162
+ input:
163
+ A -- Array
164
+ left -- first index position of the array(0 in first call)
165
+ right -- last index position of the array(len(A) - 1 in the first call)
166
+
167
+ """
161
168
if low < high :
162
169
p = partition (A , low , high )
163
170
quicksort (A , low , p - 1 )
@@ -185,8 +192,14 @@ def count(A):
185
192
186
193
###############################################################
187
194
188
-
189
195
def radix (A ):
196
+ """
197
+ Algo name: Radix Sort
198
+ STABLE
199
+ input:
200
+ A -- Array
201
+ returns sorted array
202
+ """
190
203
n = len (A )
191
204
maxelement = max (A )
192
205
digits = len (str (maxelement ))
@@ -206,10 +219,12 @@ def radix(A):
206
219
A [k ] = bins [x ].pop (0 )
207
220
k = k + 1
208
221
209
-
210
222
###############################################################
211
223
212
224
def timereq (choices , algo_name ):
225
+ """
226
+ Utility function to calculate time required(in nanoseconds) to sort the given array.
227
+ """
213
228
for c , name in zip (choices , algo_name ):
214
229
start = time .time ()
215
230
c (A )
@@ -218,44 +233,45 @@ def timereq(choices, algo_name):
218
233
219
234
###############################################################
220
235
221
- def switch_case (choice ):
222
- choices = [selection , insertion , bubble , shell , merge_driver , quick_driver , count , radix ]
223
- algo_name = ['Selection Sort' , 'Insertion Sort' , 'Bubble Sort' , 'Shell Sort' , 'Merge Sort' , 'Quick Sort' , 'Count Sort' , 'Radix Sort' ]
224
-
225
- if choice != 9 :
226
- choices [choice - 1 ](A )
227
- print ("Sorted using" , algo_name [choice - 1 ], "\n " , A )
228
- else :
229
- timereq (choices , algo_name )
230
-
231
- ###############################################################
232
-
233
- def options ():
234
- print ("Select Sorting Method" )
235
- print ("1. Selection Sort" )
236
- print ("2. Insertion Sort" )
237
- print ("3. Bubble Sort" )
238
- print ("4. Shell Sort" )
239
- print ("5. Merge Sort" )
240
- print ("6. Quick Sort" )
241
- print ('7. Count Sort' )
242
- print ('8. Radix Sort' )
243
- print ('9. Time required by each algorithm' )
244
- print ("0. Exit" )
245
-
246
236
def create_array ():
237
+ """
238
+ Return randomly generates an integer array having limit as upper limit
239
+ and amount as length of the array
240
+ """
247
241
limit = int (input ("Enter the upper limit for generating the numbers: " ))
248
242
amount = int (input ("Enter the amount of numbers you want to generate: " ))
249
243
print ("Generating numbers...\n " )
250
244
array = []
251
245
for i in range (amount ):
252
246
n = random .randint (0 , limit )
253
247
array .append (n )
254
-
255
248
return array
256
249
257
250
###############################################################
258
251
252
+ def options ():
253
+ '''
254
+ Prints Menu for operations
255
+ '''
256
+ options_list = ['Selection Sort' , 'Insertion Sort' , 'Bubble Sort' ,
257
+ 'Shell Sort' , 'Merge Sort' , 'Quick Sort' , 'Count Sort' , 'Radix Sort' ,
258
+ 'Exit' ]
259
+ print ("MENU" )
260
+ for i , option in enumerate (options_list ):
261
+ print (f'{ i + 1 } . { option } ' )
262
+
263
+ def switch_case (choice ):
264
+ choices = [selection , insertion , bubble , shell , merge_driver , quick_driver , count , radix ]
265
+ algo_name = ['Selection Sort' , 'Insertion Sort' , 'Bubble Sort' , 'Shell Sort' , 'Merge Sort' , 'Quick Sort' , 'Count Sort' , 'Radix Sort' ]
266
+
267
+ if choice != 9 :
268
+ choices [choice - 1 ](A )
269
+ print ("Sorted using" , algo_name [choice - 1 ], "\n " , A )
270
+ else :
271
+ timereq (choices , algo_name )
272
+
273
+ ###############################################################
274
+
259
275
while True :
260
276
x = input ("Enter your own array values? [y/n]: " )
261
277
if x == 'y' :
@@ -269,4 +285,4 @@ def create_array():
269
285
if choice != 0 :
270
286
switch_case (choice )
271
287
else :
272
- break
288
+ break
0 commit comments