1
1
import time
2
2
import random
3
3
4
+
4
5
def selection (A ):
5
6
"""
6
7
Algo name: Selection Sort
@@ -20,6 +21,7 @@ def selection(A):
20
21
21
22
###############################################################
22
23
24
+
23
25
def insertion (A ):
24
26
"""
25
27
Algo name: Insertion Sort
@@ -41,6 +43,7 @@ def insertion(A):
41
43
42
44
###############################################################
43
45
46
+
44
47
def bubble (A ):
45
48
"""
46
49
Algo name: Bubble Sort
@@ -58,6 +61,7 @@ def bubble(A):
58
61
59
62
###############################################################
60
63
64
+
61
65
def shell (A ):
62
66
"""
63
67
Algo name: Shell Sort
@@ -83,6 +87,7 @@ def shell(A):
83
87
84
88
###############################################################
85
89
90
+
86
91
def merge (A , l , m , r ):
87
92
"""
88
93
Algo name: Merge
@@ -95,7 +100,9 @@ def merge(A, l, m, r):
95
100
96
101
returns Sorted partial array A
97
102
"""
98
- i = l ; j = m + 1 ; k = l
103
+ i = l
104
+ j = m + 1
105
+ k = l
99
106
B = [0 ] * (r + 1 )
100
107
101
108
while i <= m and j <= r :
@@ -109,10 +116,12 @@ def merge(A, l, m, r):
109
116
110
117
while i <= m :
111
118
B [k ] = A [i ]
112
- i = i + 1 ; k = k + 1
119
+ i = i + 1
120
+ k = k + 1
113
121
while j <= r :
114
122
B [k ] = A [j ]
115
- j = j + 1 ; k = k + 1
123
+ j = j + 1
124
+ k = k + 1
116
125
for x in range (l , r + 1 ):
117
126
A [x ] = B [x ]
118
127
@@ -131,16 +140,19 @@ def mergesort(A, left, right):
131
140
mid = (left + right ) // 2
132
141
mergesort (A , left , mid )
133
142
mergesort (A , mid + 1 , right )
134
- merge (A , left , mid , right )
143
+ merge (A , left , mid , right )
144
+
135
145
136
146
def merge_driver (A ):
137
147
mergesort (A , 0 , len (A )- 1 )
138
148
139
149
###############################################################
140
150
151
+
141
152
def partition (A , low , high ):
142
153
pivot = A [low ]
143
- i = low + 1 ; j = high
154
+ i = low + 1
155
+ j = high
144
156
145
157
while True :
146
158
while i <= j and A [i ] <= pivot :
@@ -155,6 +167,7 @@ def partition(A, low, high):
155
167
A [low ], A [j ] = A [j ], A [low ]
156
168
return j
157
169
170
+
158
171
def quicksort (A , low , high ):
159
172
"""
160
173
Algo name: Quick Sort
@@ -170,18 +183,29 @@ def quicksort(A, low, high):
170
183
quicksort (A , low , p - 1 )
171
184
quicksort (A , p + 1 , high )
172
185
186
+
173
187
def quick_driver (A ):
174
188
quicksort (A , 0 , len (A )- 1 )
175
189
176
190
###############################################################
191
+
192
+
177
193
def count (A ):
194
+ """
195
+ Algo name: Count Sort
196
+ STABLE
197
+ input:
198
+ A -- Array
199
+ returns sorted array
200
+ """
178
201
n = len (A )
179
202
maxsize = max (A )
180
203
carray = [0 ] * (maxsize + 1 )
181
204
182
205
for i in range (n ):
183
206
carray [A [i ]] = carray [A [i ]] + 1
184
- i = 0 ; j = 0
207
+ i = 0
208
+ j = 0
185
209
while i < maxsize + 1 :
186
210
if carray [i ] > 0 :
187
211
A [j ] = i
@@ -192,6 +216,7 @@ def count(A):
192
216
193
217
###############################################################
194
218
219
+
195
220
def radix (A ):
196
221
"""
197
222
Algo name: Radix Sort
@@ -221,6 +246,31 @@ def radix(A):
221
246
222
247
###############################################################
223
248
249
+
250
+ def heapsort (A ):
251
+ """
252
+ Algo name: Heap Sort
253
+ UNSTABLE
254
+ input:
255
+ A -- Array
256
+ returns sorted array
257
+ """
258
+ import sys
259
+ sys .path .append ('.' )
260
+ from Heap .heap import Heap
261
+
262
+ n = len (A )
263
+ heap = Heap (n )
264
+ for i in range (n ):
265
+ heap .insert (A [i ])
266
+ k = n - 1
267
+ for _ in range (heap ._size ):
268
+ A [k ] = heap .deleteMax ()
269
+ k -= 1
270
+
271
+ ###############################################################
272
+
273
+
224
274
def timereq (choices , algo_name ):
225
275
"""
226
276
Utility function to calculate time required(in nanoseconds) to sort the given array.
@@ -233,6 +283,7 @@ def timereq(choices, algo_name):
233
283
234
284
###############################################################
235
285
286
+
236
287
def create_array ():
237
288
"""
238
289
Return randomly generates an integer array having limit as upper limit
@@ -249,22 +300,26 @@ def create_array():
249
300
250
301
###############################################################
251
302
303
+
252
304
def options ():
253
305
'''
254
306
Prints Menu for operations
255
307
'''
256
- options_list = ['Selection Sort' , 'Insertion Sort' , 'Bubble Sort' ,
257
- 'Shell Sort' , 'Merge Sort' , 'Quick Sort' , 'Count Sort' , 'Radix Sort' ,
258
- 'Time Required' , 'Exit' ]
308
+ options_list = ['Selection Sort' , 'Insertion Sort' , 'Bubble Sort' , 'Shell Sort' ,
309
+ 'Merge Sort' , 'Quick Sort' , 'Count Sort' , 'Radix Sort' , 'Heap Sort' ,
310
+ 'Time Required' , 'Exit' ]
259
311
print ("MENU" )
260
312
for i , option in enumerate (options_list ):
261
313
print (f'{ i + 1 } . { option } ' )
262
314
315
+
263
316
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 :
317
+ choices = [selection , insertion , bubble , shell , merge_driver ,
318
+ quick_driver , count , radix , heapsort ]
319
+ algo_name = ['Selection Sort' , 'Insertion Sort' , 'Bubble Sort' , 'Shell Sort' , 'Merge Sort' ,
320
+ 'Quick Sort' , 'Count Sort' , 'Radix Sort' , 'Heap Sort' ]
321
+
322
+ if choice != 10 :
268
323
choices [choice - 1 ](A )
269
324
print ("Sorted using" , algo_name [choice - 1 ], "\n " , A )
270
325
else :
@@ -283,7 +338,7 @@ def switch_case(choice):
283
338
options ()
284
339
285
340
choice = int (input ("Enter your choice: " ))
286
- if choice != 10 :
341
+ if choice != 11 :
287
342
switch_case (choice )
288
343
else :
289
344
break
0 commit comments