Skip to content

Commit ac2f642

Browse files
Refactored Sorting Algorithms Code
1 parent b4a6590 commit ac2f642

File tree

1 file changed

+47
-31
lines changed

1 file changed

+47
-31
lines changed

Sorting Algorithms/sortingAlgo.py

+47-31
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ def selection(A):
1717
if A[j] < A[position]:
1818
position = j
1919
A[i], A[position] = A[position], A[i]
20-
2120

2221
###############################################################
2322

@@ -57,7 +56,6 @@ def bubble(A):
5756
if A[i] > A[i + 1]:
5857
A[i], A[i+1] = A[i+1], A[i]
5958

60-
6159
###############################################################
6260

6361
def shell(A):
@@ -158,6 +156,15 @@ def partition(A, low, high):
158156
return j
159157

160158
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+
"""
161168
if low < high:
162169
p = partition(A, low, high)
163170
quicksort(A, low, p - 1)
@@ -185,8 +192,14 @@ def count(A):
185192

186193
###############################################################
187194

188-
189195
def radix(A):
196+
"""
197+
Algo name: Radix Sort
198+
STABLE
199+
input:
200+
A -- Array
201+
returns sorted array
202+
"""
190203
n = len(A)
191204
maxelement = max(A)
192205
digits = len(str(maxelement))
@@ -206,10 +219,12 @@ def radix(A):
206219
A[k] = bins[x].pop(0)
207220
k = k + 1
208221

209-
210222
###############################################################
211223

212224
def timereq(choices, algo_name):
225+
"""
226+
Utility function to calculate time required(in nanoseconds) to sort the given array.
227+
"""
213228
for c, name in zip(choices, algo_name):
214229
start = time.time()
215230
c(A)
@@ -218,44 +233,45 @@ def timereq(choices, algo_name):
218233

219234
###############################################################
220235

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-
246236
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+
"""
247241
limit = int(input("Enter the upper limit for generating the numbers: "))
248242
amount = int(input("Enter the amount of numbers you want to generate: "))
249243
print("Generating numbers...\n")
250244
array = []
251245
for i in range(amount):
252246
n = random.randint(0, limit)
253247
array.append(n)
254-
255248
return array
256249

257250
###############################################################
258251

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+
259275
while True:
260276
x = input("Enter your own array values? [y/n]: ")
261277
if x == 'y':
@@ -269,4 +285,4 @@ def create_array():
269285
if choice != 0:
270286
switch_case(choice)
271287
else:
272-
break
288+
break

0 commit comments

Comments
 (0)