-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsortAlgorithms.py
354 lines (317 loc) · 10.2 KB
/
sortAlgorithms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# Sorting algorithm retrieval
def get_algorithm_by_name(name, use_yield=True):
if use_yield:
if name == "Bubble Sort":
return bubble_sort
elif name == "Selection Sort":
return selection_sort
elif name == "Insertion Sort":
return insertion_sort
elif name == "Merge Sort":
return merge_sort
elif name == "Quick Sort":
return quick_sort
elif name == "Heap Sort":
return heap_sort
elif name == "Shell Sort":
return shell_sort
elif name == "Cocktail Sort":
return cocktail_sort
else:
if name == "Bubble Sort":
return bubble_sort_no_yield
elif name == "Selection Sort":
return selection_sort_no_yield
elif name == "Insertion Sort":
return insertion_sort_no_yield
elif name == "Merge Sort":
return merge_sort_no_yield
elif name == "Quick Sort":
return quick_sort_no_yield
elif name == "Heap Sort":
return heap_sort_no_yield
elif name == "Shell Sort":
return shell_sort_no_yield
elif name == "Cocktail Sort":
return cocktail_sort_no_yield
return bubble_sort_no_yield # Default
# Sorting algorithm implementations
def bubble_sort(arr):
n = len(arr)
for i in range(n):
swapped = False
for j in range(0, n - i - 1):
yield j, j + 1, False, 2 # Comparison
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
yield j, j + 1, True, 4 # Swap happened
swapped = True
if not swapped:
break
def selection_sort(arr):
n = len(arr)
for i in range(n):
min_idx = i
for j in range(i + 1, n):
yield min_idx, j, False, 2 # Comparison
if arr[j] < arr[min_idx]:
min_idx = j
if min_idx != i:
arr[i], arr[min_idx] = arr[min_idx], arr[i]
yield i, min_idx, True, 4 # Swap happened
else:
yield i, min_idx, False, 2 # No swap
def insertion_sort(arr):
n = len(arr)
for i in range(1, n):
key = arr[i]
j = i - 1
while j >= 0:
yield j, j + 1, False, 2 # Comparison
if arr[j] > key:
arr[j + 1] = arr[j]
yield j + 1, j, True, 1 # Shift
j -= 1
else:
break
arr[j + 1] = key
yield j + 1, j + 1, True, 1 # Insertion
def merge_sort(arr):
def merge_sort_rec(arr, start, end):
if end - start > 1:
mid = (start + end) // 2
yield from merge_sort_rec(arr, start, mid)
yield from merge_sort_rec(arr, mid, end)
yield from merge(arr, start, mid, end)
def merge(arr, start, mid, end):
left_subarray = arr[start:mid]
right_subarray = arr[mid:end]
left_idx, right_idx = 0, 0
current_idx = start
while left_idx < len(left_subarray) and right_idx < len(right_subarray):
yield start + left_idx, mid + right_idx, False, 2 # Comparison
if left_subarray[left_idx] <= right_subarray[right_idx]:
arr[current_idx] = left_subarray[left_idx]
left_idx += 1
else:
arr[current_idx] = right_subarray[right_idx]
right_idx += 1
yield current_idx, current_idx, True, 1 # Placement
current_idx += 1
while left_idx < len(left_subarray):
arr[current_idx] = left_subarray[left_idx]
left_idx += 1
yield current_idx, current_idx, True, 1 # Placement
current_idx += 1
while right_idx < len(right_subarray):
arr[current_idx] = right_subarray[right_idx]
right_idx += 1
yield current_idx, current_idx, True, 1 # Placement
current_idx += 1
yield from merge_sort_rec(arr, 0, len(arr))
def quick_sort(arr):
def quick_sort_rec(arr, low, high):
if low < high:
pi = yield from partition(arr, low, high)
yield from quick_sort_rec(arr, low, pi - 1)
yield from quick_sort_rec(arr, pi + 1, high)
def partition(arr, low, high):
pivot = arr[high]
i = low - 1
for j in range(low, high):
yield j, high, False, 2 # Comparison
if arr[j] < pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i]
yield i, j, True, 4 # Swap
arr[i + 1], arr[high] = arr[high], arr[i + 1]
yield i + 1, high, True, 4 # Swap pivot
return i + 1
yield from quick_sort_rec(arr, 0, len(arr) - 1)
def heap_sort(arr):
n = len(arr)
def heapify(arr, n, i):
largest = i
l = 2 * i + 1 # Left child
r = 2 * i + 2 # Right child
if l < n:
yield i, l, False, 2 # Comparison
if arr[l] > arr[largest]:
largest = l
if r < n:
yield largest, r, False, 2 # Comparison
if arr[r] > arr[largest]:
largest = r
if largest != i:
arr[i], arr[largest] = arr[largest], arr[i]
yield i, largest, True, 4 # Swap
yield from heapify(arr, n, largest)
# Build a maxheap
for i in range(n // 2 - 1, -1, -1):
yield from heapify(arr, n, i)
# One by one extract elements
for i in range(n - 1, 0, -1):
arr[i], arr[0] = arr[0], arr[i]
yield i, 0, True, 4 # Swap
yield from heapify(arr, i, 0)
def shell_sort(arr):
n = len(arr)
gap = n // 2
while gap > 0:
for i in range(gap, n):
temp = arr[i]
j = i
while j >= gap:
yield j, j - gap, False, 2 # Comparison
if arr[j - gap] > temp:
arr[j] = arr[j - gap]
yield j, j - gap, True, 1 # Shift
j -= gap
else:
break
arr[j] = temp
yield j, j, True, 1 # Insertion
gap //= 2
def cocktail_sort(arr):
n = len(arr)
swapped = True
start = 0
end = n - 1
while swapped:
swapped = False
# Traverse the array from left to right
for i in range(start, end):
yield i, i + 1, False, 2 # Comparison
if arr[i] > arr[i + 1]:
arr[i], arr[i + 1] = arr[i + 1], arr[i]
yield i, i + 1, True, 4 # Swap occurred
swapped = True
if not swapped:
break
swapped = False
end -= 1
# Traverse the array from right to left
for i in range(end - 1, start - 1, -1):
yield i, i + 1, False, 2 # Comparison
if arr[i] > arr[i + 1]:
arr[i], arr[i + 1] = arr[i + 1], arr[i]
yield i, i + 1, True, 4 # Swap occurred
swapped = True
start += 1
def bubble_sort_no_yield(arr):
n = len(arr)
for i in range(n):
swapped = False
for j in range(0, n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
swapped = True
if not swapped:
break
return arr
def selection_sort_no_yield(arr):
n = len(arr)
for i in range(n):
min_idx = i
for j in range(i + 1, n):
if arr[j] < arr[min_idx]:
min_idx = j
if min_idx != i:
arr[i], arr[min_idx] = arr[min_idx], arr[i]
return arr
def insertion_sort_no_yield(arr):
for i in range(1, len(arr)):
key = arr[i]
j = i - 1
while j >= 0 and arr[j] > key:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
return arr
def merge_sort_no_yield(arr):
if len(arr) > 1:
mid = len(arr) // 2
L = arr[:mid]
R = arr[mid:]
merge_sort_no_yield(L)
merge_sort_no_yield(R)
i = j = k = 0
while i < len(L) and j < len(R):
if L[i] < R[j]:
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1
while i < len(L):
arr[k] = L[i]
i += 1
k += 1
while j < len(R):
arr[k] = R[j]
j += 1
k += 1
return arr
def quick_sort_no_yield(arr):
if len(arr) <= 1:
return arr
else:
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quick_sort_no_yield(left) + middle + quick_sort_no_yield(right)
def heap_sort_no_yield(arr):
def heapify(arr, n, i):
largest = i
l = 2 * i + 1
r = 2 * i + 2
if l < n and arr[i] < arr[l]:
largest = l
if r < n and arr[largest] < arr[r]:
largest = r
if largest != i:
arr[i], arr[largest] = arr[largest], arr[i]
heapify(arr, n, largest)
n = len(arr)
for i in range(n//2, -1, -1):
heapify(arr, n, i)
for i in range(n-1, 0, -1):
arr[i], arr[0] = arr[0], arr[i]
heapify(arr, i, 0)
return arr
def shell_sort_no_yield(arr):
n = len(arr)
gap = n // 2
while gap > 0:
for i in range(gap, n):
temp = arr[i]
j = i
while j >= gap and arr[j - gap] > temp:
arr[j] = arr[j - gap]
j -= gap
arr[j] = temp
gap //= 2
return arr
def cocktail_sort_no_yield(arr):
n = len(arr)
swapped = True
start = 0
end = n - 1
while swapped:
swapped = False
for i in range(start, end):
if arr[i] > arr[i + 1]:
arr[i], arr[i + 1] = arr[i + 1], arr[i]
swapped = True
if not swapped:
break
swapped = False
end -= 1
for i in range(end - 1, start - 1, -1):
if arr[i] > arr[i + 1]:
arr[i], arr[i + 1] = arr[i + 1], arr[i]
swapped = True
start += 1
return arr