Skip to content

Commit

Permalink
Merge pull request #201 from benmdts/master
Browse files Browse the repository at this point in the history
Add SlowSort
  • Loading branch information
LucasPilla authored Nov 16, 2024
2 parents 8c51a00 + f937ad7 commit be7a91c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
Binary file added res/slow_sort.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/algorithms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from algorithms.insertionSort import insertionSort
from algorithms.radixSort import radixSort
from algorithms.treeSort import treeSort
from algorithms.slowSort import slowSort


__all__ = [
Expand Down Expand Up @@ -51,4 +52,5 @@
"radixSort",
"treeSort",
"exchangeSort",
"slowSort",
]
28 changes: 28 additions & 0 deletions src/algorithms/slowSort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
def slowSort(array, *args):
"""
The slow sort is an example of Multiply And Surrender a tongue-in-cheek joke of divide and conquer.
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.
Then it recursively calls the array without the previous maximum element and stores the new maximum element at the new last position.
The best case is worse than the bubble sort
Time complexity: O(N ^ ( (log N) / (2+e) ) ) where e is a small positive number
"""

def recursiveSlowSort(array, start, end):
if start >= end:
return

middle_idx = (start + end) // 2

yield from recursiveSlowSort(array, start, middle_idx)

yield from recursiveSlowSort(array, middle_idx + 1, end)

if array[end] < array[middle_idx]:
array[end], array[middle_idx] = array[middle_idx], array[end]
yield array, start, middle_idx, end, -1

yield from recursiveSlowSort(array, start, end - 1)

yield from recursiveSlowSort(array, 0, len(array) - 1)
3 changes: 2 additions & 1 deletion src/algs.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
'oddEvenSort' : oddevenSort,
'pigeonholeSort' : pigeonholeSort,
'exchangeSort' : exchangeSort,
'treeSort' : treeSort
'treeSort' : treeSort,
'slowSort' : slowSort,
}

0 comments on commit be7a91c

Please sign in to comment.