-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsorting_algorithms.py
365 lines (293 loc) · 10.9 KB
/
sorting_algorithms.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
355
356
357
358
359
360
361
362
363
364
365
from visualization import Visualization
class SortingAlgorithms:
def __init__(self) -> None:
self.visuals = Visualization()
def bubble_sort(self, draw_info, ascending=True):
lst = draw_info.list
for i in range(len(lst) - 1):
for j in range(len(lst) - 1 - i):
num1 = lst[j]
num2 = lst[j + 1]
if (num1 > num2 and ascending) or (num1 < num2 and not ascending):
lst[j], lst[j + 1] = lst[j + 1], lst[j]
self.visuals.draw_list(
draw_info,
color_positions={
j: draw_info.RED,
j + 1: draw_info.RED,
},
clear_bg=True,
)
yield True
return lst
def insertion_sort(self, draw_info, ascending=True):
lst = draw_info.list
for i in range(1, len(lst)):
current = lst[i]
while True:
ascending_sort = i > 0 and lst[i - 1] > current and ascending
descending_sort = i > 0 and lst[i - 1] < current and not ascending
if not ascending_sort and not descending_sort:
break
lst[i] = lst[i - 1]
i -= 1
lst[i] = current
self.visuals.draw_list(
draw_info,
color_positions={i: draw_info.RED, i - 1: draw_info.RED},
clear_bg=True,
)
yield True
return lst
def selection_sort(self, draw_info, ascending=True):
lst = draw_info.list
for i in range(len(lst) - 1):
min_idx = i
for j in range(i + 1, len(lst)):
if (ascending and lst[j] < lst[min_idx]) or (
not ascending and lst[j] > lst[min_idx]
):
min_idx = j
lst[i], lst[min_idx] = lst[min_idx], lst[i]
self.visuals.draw_list(
draw_info, {i: draw_info.RED, min_idx: draw_info.RED}, True
)
yield True
return lst
def merge_sort(self, draw_info, ascending=True):
def merge_sort_recursive(lst, start_idx):
if len(lst) > 1:
mid = len(lst) // 2
left_half = lst[:mid]
right_half = lst[mid:]
yield from merge_sort_recursive(left_half, start_idx)
yield from merge_sort_recursive(right_half, start_idx + mid)
i = j = k = 0
while i < len(left_half) and j < len(right_half):
if (left_half[i] < right_half[j] and ascending) or (
left_half[i] > right_half[j] and not ascending
):
lst[k] = left_half[i]
i += 1
else:
lst[k] = right_half[j]
j += 1
draw_info.list[start_idx + k] = lst[k]
self.visuals.draw_list(
draw_info, {start_idx + k: draw_info.RED}, True
)
k += 1
yield True
while i < len(left_half):
lst[k] = left_half[i]
draw_info.list[start_idx + k] = lst[k]
self.visuals.draw_list(
draw_info, {start_idx + k: draw_info.RED}, True
)
i += 1
k += 1
yield True
while j < len(right_half):
lst[k] = right_half[j]
draw_info.list[start_idx + k] = lst[k]
self.visuals.draw_list(
draw_info, {start_idx + k: draw_info.RED}, True
)
j += 1
k += 1
yield True
lst = draw_info.list
yield from merge_sort_recursive(lst, 0)
def quick_sort(self, draw_info, ascending=True):
def partition(low, high):
pivot = lst[high]
i = low - 1
for j in range(low, high):
if (lst[j] <= pivot and ascending) or (
lst[j] >= pivot and not ascending
):
i += 1
lst[i], lst[j] = lst[j], lst[i]
self.visuals.draw_list(
draw_info,
color_positions={i: draw_info.RED, j: draw_info.RED},
clear_bg=True,
)
yield True
lst[i + 1], lst[high] = lst[high], lst[i + 1]
self.visuals.draw_list(
draw_info,
color_positions={i + 1: draw_info.RED, high: draw_info.RED},
clear_bg=True,
)
yield True
return i + 1
def quick_sort_recursive(low, high):
if low < high:
partition_index = yield from partition(low, high)
yield from quick_sort_recursive(low, partition_index - 1)
yield from quick_sort_recursive(partition_index + 1, high)
lst = draw_info.list
yield from quick_sort_recursive(0, len(lst) - 1)
def heap_sort(self, draw_info, ascending=True):
def heapify(n, i):
largest = i
l = 2 * i + 1
r = 2 * i + 2
if l < n and (
(lst[l] > lst[largest] and ascending)
or (lst[l] < lst[largest] and not ascending)
):
largest = l
if r < n and (
(lst[r] > lst[largest] and ascending)
or (lst[l] < lst[largest] and not ascending)
):
largest = r
if largest != i:
lst[i], lst[largest] = lst[largest], lst[i]
self.visuals.draw_list(
draw_info,
color_positions={i: draw_info.RED, largest: draw_info.RED},
clear_bg=True,
)
yield True
yield from heapify(n, largest)
lst = draw_info.list
n = len(lst)
for i in range(n // 2 - 1, -1, -1):
yield from heapify(n, i)
for i in range(n - 1, 0, -1):
lst[i], lst[0] = lst[0], lst[i]
self.visuals.draw_list(
draw_info,
color_positions={i: draw_info.RED, 0: draw_info.RED},
clear_bg=True,
)
yield True
yield from heapify(i, 0)
return lst
def counting_sort(self, draw_info, ascending=True):
lst = draw_info.list
max_value = max(lst)
min_value = min(lst)
range_of_element = max_value - min_value + 1
count = [0] * range_of_element
output = [0] * len(lst)
for num in lst:
count[num - min_value] += 1
if ascending:
for i in range(1, len(count)):
count[i] += count[i - 1]
else:
for i in range(len(count) - 2, -1, -1):
count[i] += count[i + 1]
for i in range(len(lst) - 1, -1, -1):
output[count[lst[i] - min_value] - 1] = lst[i]
count[lst[i] - min_value] -= 1
self.visuals.draw_list(
draw_info,
color_positions={
i: draw_info.RED,
},
clear_bg=True,
)
yield True
for i in range(len(lst)):
lst[i] = output[i]
self.visuals.draw_list(
draw_info, color_positions={i: draw_info.RED}, clear_bg=True
)
yield True
return lst
def radix_sort(self, draw_info, ascending=True):
def counting_sort_exp(arr, exp):
n = len(arr)
output = [0] * n
count = [0] * 10
for i in range(n):
index = arr[i] // exp
count[index % 10] += 1
if ascending:
for i in range(1, 10):
count[i] += count[i - 1]
else:
for i in range(8, -1, -1):
count[i] += count[i + 1]
i = n - 1
while i >= 0:
index = arr[i] // exp
output[count[index % 10] - 1] = arr[i]
count[index % 10] -= 1
self.visuals.draw_list(
draw_info,
color_positions={i: draw_info.RED},
clear_bg=True,
)
yield True
i -= 1
for i in range(n):
arr[i] = output[i]
lst = draw_info.list
max_value = max(lst)
exp = 1
while max_value // exp > 0:
yield from counting_sort_exp(lst, exp)
exp *= 10
return lst
def comb_sort(self, draw_info, ascending=True):
def get_next_gap(gap):
gap = (gap * 10) // 13
if gap < 1:
return 1
return gap
lst = draw_info.list
n = len(lst)
gap = n
swapped = True
while gap != 1 or swapped:
gap = get_next_gap(gap)
swapped = False
for i in range(n - gap):
if (ascending and lst[i] > lst[i + gap]) or (
not ascending and lst[i] < lst[i + gap]
):
lst[i], lst[i + gap] = lst[i + gap], lst[i]
swapped = True
self.visuals.draw_list(
draw_info,
color_positions={
i: draw_info.RED,
i + gap: draw_info.RED,
},
clear_bg=True,
)
yield True
return lst
def shell_sort(self, draw_info, ascending=True):
lst = draw_info.list
n = len(lst)
gap = n // 2
while gap > 0:
for i in range(gap, n):
temp = lst[i]
j = i
while j >= gap and (
(ascending and lst[j - gap] > temp)
or (not ascending and lst[j - gap] < temp)
):
lst[j] = lst[j - gap]
j -= gap
self.visuals.draw_list(
draw_info,
color_positions={j: draw_info.RED, j + gap: draw_info.RED},
clear_bg=True,
)
yield True
lst[j] = temp
self.visuals.draw_list(
draw_info, color_positions={j: draw_info.RED}, clear_bg=True
)
yield True
gap //= 2
return lst