-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorting_algorithms.py
More file actions
777 lines (637 loc) · 21.8 KB
/
sorting_algorithms.py
File metadata and controls
777 lines (637 loc) · 21.8 KB
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
"""
Sorting Algorithms Implementation
Contains implementations of various sorting algorithms for educational purposes.
"""
import bisect
def bubble_sort(arr):
"""Bubble Sort - O(n²) time, O(1) space"""
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(arr):
"""Selection Sort - O(n²) time, O(1) space"""
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
arr[i], arr[min_idx] = arr[min_idx], arr[i]
return arr
def insertion_sort(arr):
"""Insertion Sort - O(n²) time, O(1) space"""
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(arr):
"""Merge Sort - O(n log n) time, O(n) space"""
if len(arr) <= 1:
return arr
mid = len(arr) // 2
left = merge_sort(arr[:mid])
right = merge_sort(arr[mid:])
return merge(left, right)
def merge(left, right):
"""Helper function for merge_sort"""
result = []
i = j = 0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
result.extend(left[i:])
result.extend(right[j:])
return result
def quick_sort(arr):
"""Quick Sort - O(n log n) average, O(n²) worst, O(log n) space"""
def quick_sort_helper(arr, low, high):
if low < high:
pi = partition(arr, low, high)
quick_sort_helper(arr, low, pi - 1)
quick_sort_helper(arr, pi + 1, high)
quick_sort_helper(arr, 0, len(arr) - 1)
return arr
def partition(arr, low, high):
"""Helper function for quick_sort"""
pivot = arr[high]
i = low - 1
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i]
arr[i + 1], arr[high] = arr[high], arr[i + 1]
return i + 1
def heap_sort(arr):
"""Heap Sort - O(n log n) time, O(1) space"""
n = len(arr)
# Build max heap
for i in range(n // 2 - 1, -1, -1):
heapify(arr, n, i)
# Extract elements one by one
for i in range(n - 1, 0, -1):
arr[0], arr[i] = arr[i], arr[0]
heapify(arr, i, 0)
return arr
def heapify(arr, n, i):
"""Helper function for heap_sort"""
largest = i
left = 2 * i + 1
right = 2 * i + 2
if left < n and arr[left] > arr[largest]:
largest = left
if right < n and arr[right] > arr[largest]:
largest = right
if largest != i:
arr[i], arr[largest] = arr[largest], arr[i]
heapify(arr, n, largest)
def counting_sort(arr):
"""Counting Sort - O(n+k) time, O(k) space"""
if not arr:
return arr
min_val = min(arr)
max_val = max(arr)
range_val = max_val - min_val + 1
# Create count array
count = [0] * range_val
# Store count of each element
for num in arr:
count[num - min_val] += 1
# Build output array
result = []
for i in range(range_val):
result.extend([i + min_val] * count[i])
return result
def radix_sort(arr):
"""Radix Sort - O(d·(n+k)) time, O(n+k) space"""
if not arr:
return arr
# Handle negative numbers
max_abs = max(abs(x) for x in arr)
exp = 1
# Separate positive and negative numbers
positives = [x for x in arr if x >= 0]
negatives = [-x for x in arr if x < 0]
# Sort positive numbers
while max_abs // exp > 0:
positives = counting_sort_by_digit(positives, exp)
exp *= 10
# Sort negative numbers and reverse
max_neg = max(negatives) if negatives else 0
exp = 1
while max_neg // exp > 0:
negatives = counting_sort_by_digit(negatives, exp)
exp *= 10
# Combine results: negative numbers (reversed and negated) + positive numbers
negatives = [-x for x in reversed(negatives)]
return negatives + positives
def counting_sort_by_digit(arr, exp):
"""Helper function for radix_sort - sorts by specific digit"""
n = len(arr)
output = [0] * n
count = [0] * 10
# Store count of occurrences
for i in range(n):
index = (arr[i] // exp) % 10
count[index] += 1
# Change count[i] so that count[i] contains actual position
for i in range(1, 10):
count[i] += count[i - 1]
# Build output array
for i in range(n - 1, -1, -1):
index = (arr[i] // exp) % 10
output[count[index] - 1] = arr[i]
count[index] -= 1
return output
def bucket_sort(arr):
"""Bucket Sort - O(n+k) average time, O(n+k) space"""
if not arr:
return arr
# Find minimum and maximum values
min_val = min(arr)
max_val = max(arr)
# Create buckets
bucket_count = len(arr)
buckets = [[] for _ in range(bucket_count)]
# Distribute input array values into buckets
for num in arr:
# Normalize to [0, 1] range
normalized = (num - min_val) / (max_val - min_val) if max_val != min_val else 0
bucket_index = int(normalized * (bucket_count - 1))
buckets[bucket_index].append(num)
# Sort individual buckets and concatenate
result = []
for bucket in buckets:
if bucket:
bucket.sort() # Using Python's built-in sort for individual buckets
result.extend(bucket)
return result
def shell_sort(arr):
"""Shell Sort - O(n^(3/2)) average time, O(1) space"""
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 tim_sort(arr):
"""Tim Sort - Python's actual algorithm implementation"""
if len(arr) < 2:
return arr[:]
MIN_MERGE = 32
def calc_min_run(n):
"""Calculate minimum run length for Tim Sort"""
r = 0
while n >= MIN_MERGE:
r |= n & 1
n >>= 1
return n + r
def binary_sort(a, lo, hi, start):
if start == lo:
start += 1
for i in range(start, hi):
pivot = a[i]
pos = bisect.bisect_right(a, pivot, lo, i)
j = i
while j > pos:
a[j] = a[j - 1]
j -= 1
a[pos] = pivot
def count_run_and_make_ascending(a, lo, hi):
run_hi = lo + 1
if run_hi == hi:
return 1
if a[run_hi] < a[lo]:
run_hi += 1
while run_hi < hi and a[run_hi] < a[run_hi - 1]:
run_hi += 1
a[lo:run_hi] = reversed(a[lo:run_hi])
else:
run_hi += 1
while run_hi < hi and a[run_hi] >= a[run_hi - 1]:
run_hi += 1
return run_hi - lo
def gallop_left(key, a, base, length, hint):
ofs = 1
last_ofs = 0
if key > a[base + hint]:
max_ofs = length - hint
while ofs < max_ofs and key > a[base + hint + ofs]:
last_ofs = ofs
ofs = (ofs << 1) + 1
if ofs <= 0:
ofs = max_ofs
if ofs > max_ofs:
ofs = max_ofs
last_ofs += hint
ofs += hint
else:
max_ofs = hint + 1
while ofs < max_ofs and key <= a[base + hint - ofs]:
last_ofs = ofs
ofs = (ofs << 1) + 1
if ofs <= 0:
ofs = max_ofs
if ofs > max_ofs:
ofs = max_ofs
tmp = last_ofs
last_ofs = hint - ofs
ofs = hint - tmp
last_ofs += 1
while last_ofs < ofs:
m = last_ofs + ((ofs - last_ofs) >> 1)
if key > a[base + m]:
last_ofs = m + 1
else:
ofs = m
return ofs
def gallop_right(key, a, base, length, hint):
ofs = 1
last_ofs = 0
if key < a[base + hint]:
max_ofs = hint + 1
while ofs < max_ofs and key < a[base + hint - ofs]:
last_ofs = ofs
ofs = (ofs << 1) + 1
if ofs <= 0:
ofs = max_ofs
if ofs > max_ofs:
ofs = max_ofs
tmp = last_ofs
last_ofs = hint - ofs
ofs = hint - tmp
else:
max_ofs = length - hint
while ofs < max_ofs and key >= a[base + hint + ofs]:
last_ofs = ofs
ofs = (ofs << 1) + 1
if ofs <= 0:
ofs = max_ofs
if ofs > max_ofs:
ofs = max_ofs
last_ofs += hint
ofs += hint
last_ofs += 1
while last_ofs < ofs:
m = last_ofs + ((ofs - last_ofs) >> 1)
if key < a[base + m]:
ofs = m
else:
last_ofs = m + 1
return ofs
MIN_GALLOP = 7
class _TimSortState:
def __init__(self, a):
self.a = a
self.min_gallop = MIN_GALLOP
self.run_base = []
self.run_len = []
def push_run(self, base, length):
self.run_base.append(base)
self.run_len.append(length)
def merge_collapse(self):
while len(self.run_len) > 1:
n = len(self.run_len) - 2
if (
(n > 0 and self.run_len[n - 1] <= self.run_len[n] + self.run_len[n + 1])
or (n > 1 and self.run_len[n - 2] <= self.run_len[n - 1] + self.run_len[n])
):
if self.run_len[n - 1] < self.run_len[n + 1]:
n -= 1
self.merge_at(n)
elif self.run_len[n] <= self.run_len[n + 1]:
self.merge_at(n)
else:
break
def merge_force_collapse(self):
while len(self.run_len) > 1:
n = len(self.run_len) - 2
if n > 0 and self.run_len[n - 1] < self.run_len[n + 1]:
n -= 1
self.merge_at(n)
def merge_at(self, i):
a = self.a
base1 = self.run_base[i]
len1 = self.run_len[i]
base2 = self.run_base[i + 1]
len2 = self.run_len[i + 1]
self.run_len[i] = len1 + len2
if i == len(self.run_len) - 3:
self.run_base[i + 1] = self.run_base[i + 2]
self.run_len[i + 1] = self.run_len[i + 2]
self.run_base.pop()
self.run_len.pop()
k = gallop_right(a[base2], a, base1, len1, 0)
base1 += k
len1 -= k
if len1 == 0:
return
len2 = gallop_left(a[base1 + len1 - 1], a, base2, len2, len2 - 1)
if len2 == 0:
return
if len1 <= len2:
self.merge_lo(base1, len1, base2, len2)
else:
self.merge_hi(base1, len1, base2, len2)
def merge_lo(self, base1, len1, base2, len2):
a = self.a
tmp = a[base1 : base1 + len1]
cursor1 = 0
cursor2 = base2
dest = base1
a[dest] = a[cursor2]
dest += 1
cursor2 += 1
len2 -= 1
if len2 == 0:
a[dest : dest + len1] = tmp[cursor1 : cursor1 + len1]
return
if len1 == 1:
a[dest : dest + len2] = a[cursor2 : cursor2 + len2]
a[dest + len2] = tmp[cursor1]
return
min_gallop = self.min_gallop
while True:
count1 = 0
count2 = 0
while True:
if a[cursor2] < tmp[cursor1]:
a[dest] = a[cursor2]
dest += 1
cursor2 += 1
count2 += 1
count1 = 0
len2 -= 1
if len2 == 0:
break
else:
a[dest] = tmp[cursor1]
dest += 1
cursor1 += 1
count1 += 1
count2 = 0
len1 -= 1
if len1 == 1:
break
if (count1 | count2) >= min_gallop:
break
if len2 == 0 or len1 == 1:
break
while True:
count1 = gallop_right(a[cursor2], tmp, cursor1, len1, 0)
if count1 != 0:
a[dest : dest + count1] = tmp[cursor1 : cursor1 + count1]
dest += count1
cursor1 += count1
len1 -= count1
if len1 <= 1:
break
a[dest] = a[cursor2]
dest += 1
cursor2 += 1
len2 -= 1
if len2 == 0:
break
count2 = gallop_left(tmp[cursor1], a, cursor2, len2, 0)
if count2 != 0:
a[dest : dest + count2] = a[cursor2 : cursor2 + count2]
dest += count2
cursor2 += count2
len2 -= count2
if len2 == 0:
break
a[dest] = tmp[cursor1]
dest += 1
cursor1 += 1
len1 -= 1
if len1 == 1:
break
min_gallop -= 1
if not (count1 >= MIN_GALLOP or count2 >= MIN_GALLOP):
break
if min_gallop < 0:
min_gallop = 0
min_gallop += 2
if len2 == 0 or len1 == 1:
break
self.min_gallop = max(1, min_gallop)
if len1 == 1:
a[dest : dest + len2] = a[cursor2 : cursor2 + len2]
a[dest + len2] = tmp[cursor1]
elif len1 > 0:
a[dest : dest + len1] = tmp[cursor1 : cursor1 + len1]
def merge_hi(self, base1, len1, base2, len2):
a = self.a
tmp = a[base2 : base2 + len2]
cursor1 = base1 + len1 - 1
cursor2 = len2 - 1
dest = base2 + len2 - 1
a[dest] = a[cursor1]
dest -= 1
cursor1 -= 1
len1 -= 1
if len1 == 0:
a[dest - len2 + 1 : dest + 1] = tmp[0:len2]
return
if len2 == 1:
dest -= len1
cursor1 -= len1
a[dest + 1 : dest + 1 + len1] = a[cursor1 + 1 : cursor1 + 1 + len1]
a[dest] = tmp[cursor2]
return
min_gallop = self.min_gallop
while True:
count1 = 0
count2 = 0
while True:
if tmp[cursor2] < a[cursor1]:
a[dest] = a[cursor1]
dest -= 1
cursor1 -= 1
count1 += 1
count2 = 0
len1 -= 1
if len1 == 0:
break
else:
a[dest] = tmp[cursor2]
dest -= 1
cursor2 -= 1
count2 += 1
count1 = 0
len2 -= 1
if len2 == 1:
break
if (count1 | count2) >= min_gallop:
break
if len1 == 0 or len2 == 1:
break
while True:
count1 = len1 - gallop_right(tmp[cursor2], a, base1, len1, len1 - 1)
if count1 != 0:
dest -= count1
cursor1 -= count1
len1 -= count1
a[dest + 1 : dest + 1 + count1] = a[cursor1 + 1 : cursor1 + 1 + count1]
if len1 == 0:
break
a[dest] = tmp[cursor2]
dest -= 1
cursor2 -= 1
len2 -= 1
if len2 == 1:
break
count2 = len2 - gallop_left(a[cursor1], tmp, 0, len2, len2 - 1)
if count2 != 0:
dest -= count2
cursor2 -= count2
len2 -= count2
a[dest + 1 : dest + 1 + count2] = tmp[cursor2 + 1 : cursor2 + 1 + count2]
if len2 <= 1:
break
a[dest] = a[cursor1]
dest -= 1
cursor1 -= 1
len1 -= 1
if len1 == 0:
break
min_gallop -= 1
if not (count1 >= MIN_GALLOP or count2 >= MIN_GALLOP):
break
if min_gallop < 0:
min_gallop = 0
min_gallop += 2
if len1 == 0 or len2 == 1:
break
self.min_gallop = max(1, min_gallop)
if len2 == 1:
dest -= len1
cursor1 -= len1
a[dest + 1 : dest + 1 + len1] = a[cursor1 + 1 : cursor1 + 1 + len1]
a[dest] = tmp[cursor2]
elif len2 > 0:
a[dest - len2 + 1 : dest + 1] = tmp[0:len2]
n = len(arr)
min_run = calc_min_run(n)
state = _TimSortState(arr)
lo = 0
remaining = n
while remaining:
run_len = count_run_and_make_ascending(arr, lo, n)
if run_len < min_run:
force = min(min_run, remaining)
binary_sort(arr, lo, lo + force, lo + run_len)
run_len = force
state.push_run(lo, run_len)
state.merge_collapse()
lo += run_len
remaining -= run_len
state.merge_force_collapse()
return arr
def intro_sort(arr):
"""Intro Sort - C++ std::sort algorithm implementation"""
if len(arr) < 2:
return arr[:]
# Intro Sort parameters
MAX_DEPTH = 2 * (len(arr).bit_length()) # 2 * log2(n)
def insertion_sort(arr, left, right):
"""Insertion sort for small arrays"""
for i in range(left + 1, right + 1):
key = arr[i]
j = i - 1
while j >= left and arr[j] > key:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
def heapify(arr, n, i):
"""Heapify helper for heap sort"""
largest = i
left = 2 * i + 1
right = 2 * i + 2
if left < n and arr[left] > arr[largest]:
largest = left
if right < n and arr[right] > arr[largest]:
largest = right
if largest != i:
arr[i], arr[largest] = arr[largest], arr[i]
heapify(arr, n, largest)
def heap_sort(arr, left, right):
"""Heap sort for fallback"""
n = right - left + 1
# Build max heap
for i in range(n // 2 - 1, -1, -1):
heapify(arr, left + n, left + i)
# Extract elements
for i in range(n - 1, 0, -1):
arr[left], arr[left + i] = arr[left + i], arr[left]
heapify(arr, left + i, left)
def partition(arr, low, high):
"""Partition for quick sort"""
pivot = arr[high]
i = low - 1
for j in range(low, high):
if arr[j] <= pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i]
arr[i + 1], arr[high] = arr[high], arr[i + 1]
return i + 1
def intro_sort_helper(arr, left, right, depth):
"""Recursive intro sort helper"""
if right - left <= 16: # Use insertion sort for small arrays
insertion_sort(arr, left, right)
elif depth == 0: # Use heap sort when depth limit reached
heap_sort(arr, left, right)
else:
pivot = partition(arr, left, right)
intro_sort_helper(arr, left, pivot - 1, depth - 1)
intro_sort_helper(arr, pivot + 1, right, depth - 1)
intro_sort_helper(arr, 0, len(arr) - 1, MAX_DEPTH)
return arr
def tree_sort(arr):
"""Tree Sort - O(n log n) time, O(n) space"""
class TreeNode:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def insert(root, val):
if root is None:
return TreeNode(val)
if val < root.val:
root.left = insert(root.left, val)
else:
root.right = insert(root.right, val)
return root
def inorder(root, result):
if root:
inorder(root.left, result)
result.append(root.val)
inorder(root.right, result)
if not arr:
return arr
root = None
for num in arr:
root = insert(root, num)
result = []
inorder(root, result)
return result