Skip to content

Commit be77f76

Browse files
Added Heapsort
1 parent 5d5bcdc commit be77f76

File tree

1 file changed

+69
-14
lines changed

1 file changed

+69
-14
lines changed

Sorting Algorithms/sortingAlgo.py

Lines changed: 69 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import time
22
import random
33

4+
45
def selection(A):
56
"""
67
Algo name: Selection Sort
@@ -20,6 +21,7 @@ def selection(A):
2021

2122
###############################################################
2223

24+
2325
def insertion(A):
2426
"""
2527
Algo name: Insertion Sort
@@ -41,6 +43,7 @@ def insertion(A):
4143

4244
###############################################################
4345

46+
4447
def bubble(A):
4548
"""
4649
Algo name: Bubble Sort
@@ -58,6 +61,7 @@ def bubble(A):
5861

5962
###############################################################
6063

64+
6165
def shell(A):
6266
"""
6367
Algo name: Shell Sort
@@ -83,6 +87,7 @@ def shell(A):
8387

8488
###############################################################
8589

90+
8691
def merge(A, l, m, r):
8792
"""
8893
Algo name: Merge
@@ -95,7 +100,9 @@ def merge(A, l, m, r):
95100
96101
returns Sorted partial array A
97102
"""
98-
i = l; j = m + 1; k = l
103+
i = l
104+
j = m + 1
105+
k = l
99106
B = [0] * (r + 1)
100107

101108
while i <= m and j <= r:
@@ -109,10 +116,12 @@ def merge(A, l, m, r):
109116

110117
while i <= m:
111118
B[k] = A[i]
112-
i = i + 1; k = k + 1
119+
i = i + 1
120+
k = k + 1
113121
while j <= r:
114122
B[k] = A[j]
115-
j = j + 1; k = k + 1
123+
j = j + 1
124+
k = k + 1
116125
for x in range(l, r + 1):
117126
A[x] = B[x]
118127

@@ -131,16 +140,19 @@ def mergesort(A, left, right):
131140
mid = (left + right) // 2
132141
mergesort(A, left, mid)
133142
mergesort(A, mid + 1, right)
134-
merge(A, left, mid, right)
143+
merge(A, left, mid, right)
144+
135145

136146
def merge_driver(A):
137147
mergesort(A, 0, len(A)-1)
138148

139149
###############################################################
140150

151+
141152
def partition(A, low, high):
142153
pivot = A[low]
143-
i = low + 1; j = high
154+
i = low + 1
155+
j = high
144156

145157
while True:
146158
while i <= j and A[i] <= pivot:
@@ -155,6 +167,7 @@ def partition(A, low, high):
155167
A[low], A[j] = A[j], A[low]
156168
return j
157169

170+
158171
def quicksort(A, low, high):
159172
"""
160173
Algo name: Quick Sort
@@ -170,18 +183,29 @@ def quicksort(A, low, high):
170183
quicksort(A, low, p - 1)
171184
quicksort(A, p + 1, high)
172185

186+
173187
def quick_driver(A):
174188
quicksort(A, 0, len(A)-1)
175189

176190
###############################################################
191+
192+
177193
def count(A):
194+
"""
195+
Algo name: Count Sort
196+
STABLE
197+
input:
198+
A -- Array
199+
returns sorted array
200+
"""
178201
n = len(A)
179202
maxsize = max(A)
180203
carray = [0] * (maxsize + 1)
181204

182205
for i in range(n):
183206
carray[A[i]] = carray[A[i]] + 1
184-
i = 0; j = 0
207+
i = 0
208+
j = 0
185209
while i < maxsize + 1:
186210
if carray[i] > 0:
187211
A[j] = i
@@ -192,6 +216,7 @@ def count(A):
192216

193217
###############################################################
194218

219+
195220
def radix(A):
196221
"""
197222
Algo name: Radix Sort
@@ -221,6 +246,31 @@ def radix(A):
221246

222247
###############################################################
223248

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+
224274
def timereq(choices, algo_name):
225275
"""
226276
Utility function to calculate time required(in nanoseconds) to sort the given array.
@@ -233,6 +283,7 @@ def timereq(choices, algo_name):
233283

234284
###############################################################
235285

286+
236287
def create_array():
237288
"""
238289
Return randomly generates an integer array having limit as upper limit
@@ -249,22 +300,26 @@ def create_array():
249300

250301
###############################################################
251302

303+
252304
def options():
253305
'''
254306
Prints Menu for operations
255307
'''
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']
259311
print("MENU")
260312
for i, option in enumerate(options_list):
261313
print(f'{i + 1}. {option}')
262314

315+
263316
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:
268323
choices[choice-1](A)
269324
print("Sorted using", algo_name[choice - 1], "\n", A)
270325
else:
@@ -283,7 +338,7 @@ def switch_case(choice):
283338
options()
284339

285340
choice = int(input("Enter your choice: "))
286-
if choice != 10:
341+
if choice != 11:
287342
switch_case(choice)
288343
else:
289344
break

0 commit comments

Comments
 (0)