-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #201 from benmdts/master
Add SlowSort
- Loading branch information
Showing
4 changed files
with
32 additions
and
1 deletion.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters