Skip to content

Commit f937ad7

Browse files
committedNov 13, 2024
Add SlowSort
1 parent 8c51a00 commit f937ad7

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed
 

‎res/slow_sort.gif

1010 KB
Loading

‎src/algorithms/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from algorithms.insertionSort import insertionSort
2424
from algorithms.radixSort import radixSort
2525
from algorithms.treeSort import treeSort
26+
from algorithms.slowSort import slowSort
2627

2728

2829
__all__ = [
@@ -51,4 +52,5 @@
5152
"radixSort",
5253
"treeSort",
5354
"exchangeSort",
55+
"slowSort",
5456
]

‎src/algorithms/slowSort.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
def slowSort(array, *args):
2+
"""
3+
The slow sort is an example of Multiply And Surrender a tongue-in-cheek joke of divide and conquer.
4+
Slow sort stores the maximum element of the array at the last position by recursively divides the array by half and compares each of them.
5+
Then it recursively calls the array without the previous maximum element and stores the new maximum element at the new last position.
6+
The best case is worse than the bubble sort
7+
8+
Time complexity: O(N ^ ( (log N) / (2+e) ) ) where e is a small positive number
9+
10+
"""
11+
12+
def recursiveSlowSort(array, start, end):
13+
if start >= end:
14+
return
15+
16+
middle_idx = (start + end) // 2
17+
18+
yield from recursiveSlowSort(array, start, middle_idx)
19+
20+
yield from recursiveSlowSort(array, middle_idx + 1, end)
21+
22+
if array[end] < array[middle_idx]:
23+
array[end], array[middle_idx] = array[middle_idx], array[end]
24+
yield array, start, middle_idx, end, -1
25+
26+
yield from recursiveSlowSort(array, start, end - 1)
27+
28+
yield from recursiveSlowSort(array, 0, len(array) - 1)

‎src/algs.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
'oddEvenSort' : oddevenSort,
2727
'pigeonholeSort' : pigeonholeSort,
2828
'exchangeSort' : exchangeSort,
29-
'treeSort' : treeSort
29+
'treeSort' : treeSort,
30+
'slowSort' : slowSort,
3031
}
3132

0 commit comments

Comments
 (0)
Failed to load comments.