Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 37c756a

Browse files
Refactored Code
1 parent 07c2a20 commit 37c756a

File tree

1 file changed

+83
-44
lines changed

1 file changed

+83
-44
lines changed

‎sortingAlgo.py‎

Lines changed: 83 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import time
2+
import random
23

34
def selection(A):
45
"""
@@ -134,6 +135,8 @@ def mergesort(A, left, right):
134135
mergesort(A, mid + 1, right)
135136
merge(A, left, mid, right)
136137

138+
def merge_driver(A):
139+
mergesort(A, 0, len(A)-1)
137140

138141
###############################################################
139142

@@ -160,45 +163,70 @@ def quicksort(A, low, high):
160163
quicksort(A, low, p - 1)
161164
quicksort(A, p + 1, high)
162165

166+
def quick_driver(A):
167+
quicksort(A, 0, len(A)-1)
168+
163169
###############################################################
164-
def switch_case(choice):
170+
def count(A):
171+
n = len(A)
172+
maxsize = max(A)
173+
carray = [0] * (maxsize + 1)
174+
175+
for i in range(n):
176+
carray[A[i]] = carray[A[i]] + 1
177+
i = 0; j = 0
178+
while i < maxsize + 1:
179+
if carray[i] > 0:
180+
A[j] = i
181+
j = j + 1
182+
carray[i] = carray[i] - 1
183+
else:
184+
i = i + 1
165185

166-
if choice == 1:
167-
start = time.time()
168-
selection(A)
169-
end = time.time()
170-
print("Sorted using Selection Sort: ", A)
171-
print('Time required: ', (end - start) * 10 ** 6, "nanoseconds")
172-
elif choice == 2:
173-
start = time.time()
174-
insertion(A)
175-
end = time.time()
176-
print("Sorted using Insertion Sort: ", A)
177-
print('Time required: ', (end - start) * 10 ** 6, "nanoseconds")
178-
elif choice == 3:
179-
start = time.time()
180-
bubble(A)
181-
end = time.time()
182-
print("Sorted using Bubble Sort: ", A)
183-
print('Time required: ', (end - start) * 10 ** 6, "nanoseconds")
184-
elif choice == 4:
185-
start = time.time()
186-
shell(A)
187-
end = time.time()
188-
print("Sorted using Shell Sort: ", A)
189-
print('Time required: ', (end - start) * 10 ** 6, "nanoseconds")
190-
elif choice == 5:
191-
start = time.time()
192-
mergesort(A, 0, len(A)-1)
193-
end = time.time()
194-
print("Sorted using Merge Sort: ", A)
195-
print('Time required: ', (end - start) * 10 ** 6, "nanoseconds")
196-
elif choice == 6:
186+
###############################################################
187+
188+
189+
def radix(A):
190+
n = len(A)
191+
maxelement = max(A)
192+
digits = len(str(maxelement))
193+
l = []
194+
bins = [l] * 10
195+
for i in range(digits):
196+
for j in range(n):
197+
e = int((A[j] / pow(10, i)) % 10)
198+
if len(bins[e]) > 0:
199+
bins[e].append(A[j])
200+
else:
201+
bins[e] = [A[j]]
202+
k = 0
203+
for x in range(10):
204+
if len(bins[x]) > 0:
205+
for y in range(len(bins[x])):
206+
A[k] = bins[x].pop(0)
207+
k = k + 1
208+
209+
210+
###############################################################
211+
212+
def timereq(choices, algo_name):
213+
for c, name in zip(choices, algo_name):
197214
start = time.time()
198-
quicksort(A, 0, len(A)-1)
215+
c(A)
199216
end = time.time()
200-
print("Sorted using Quick Sort: ", A)
201-
print('Time required: ', (end - start) * 10 ** 6, "nanoseconds")
217+
print('Time taken by', name, ':', (end - start) * 10 ** 6, "nanoseconds")
218+
219+
###############################################################
220+
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)
202230

203231
###############################################################
204232

@@ -210,24 +238,35 @@ def options():
210238
print("4. Shell Sort")
211239
print("5. Merge Sort")
212240
print("6. Quick Sort")
213-
print("#. Exit")
241+
print('7. Count Sort')
242+
print('8. Radix Sort')
243+
print('9. Time required by each algorithm')
244+
print("0. Exit")
245+
246+
def create_array():
247+
limit = int(input("Enter the upper limit for generating the numbers: "))
248+
amount = int(input("Enter the amount of numbers you want to generate: "))
249+
print("Generating numbers...\n")
250+
array = []
251+
for i in range(amount):
252+
n = random.randint(0, limit)
253+
array.append(n)
254+
255+
return array
256+
257+
###############################################################
214258

215259
while True:
216260
x = input("Enter your own array values? [y/n]: ")
217261
if x == 'y':
218262
A = list(map(int, input("Enter values: ").split()))
219263
options()
220264
else:
221-
filename = "num.txt"
222-
my_file = open(filename, "r")
223-
content = my_file.read()
224-
A = list(map(int, content.split()))
225-
my_file.close()
226-
print("Reading values from", filename)
265+
A = create_array()
227266
options()
228267

229268
choice = int(input("Enter your choice: "))
230-
if choice != '#':
269+
if choice != 0:
231270
switch_case(choice)
232271
else:
233-
break
272+
break

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /