|
1 | | -def stooge_sort(arr: list[int]) -> list[int]: |
2 | | - """ |
3 | | - Examples: |
4 | | - >>> stooge_sort([18.1, 0, -7.1, -1, 2, 2]) |
5 | | - [-7.1, -1, 0, 2, 2, 18.1] |
6 | | -
|
7 | | - >>> stooge_sort([]) |
8 | | - [] |
9 | | - """ |
10 | | - stooge(arr, 0, len(arr) - 1) |
11 | | - return arr |
12 | | - |
13 | | - |
14 | | -def stooge(arr: list[int], i: int, h: int) -> None: |
15 | | - if i >= h: |
16 | | - return |
17 | | - |
18 | | - # If first element is smaller than the last then swap them |
19 | | - if arr[i] > arr[h]: |
20 | | - arr[i], arr[h] = arr[h], arr[i] |
21 | | - |
22 | | - # If there are more than 2 elements in the array |
23 | | - if h - i + 1 > 2: |
24 | | - t = (int)((h - i + 1) / 3) |
25 | | - |
26 | | - # Recursively sort first 2/3 elements |
27 | | - stooge(arr, i, (h - t)) |
28 | | - |
29 | | - # Recursively sort last 2/3 elements |
30 | | - stooge(arr, i + t, (h)) |
31 | | - |
32 | | - # Recursively sort first 2/3 elements |
33 | | - stooge(arr, i, (h - t)) |
34 | | - |
35 | | - |
36 | | -if __name__ == "__main__": |
37 | | - user_input = input("Enter numbers separated by a comma:\n").strip() |
38 | | - unsorted = [int(item) for item in user_input.split(",")] |
39 | | - print(stooge_sort(unsorted)) |
| 1 | +""" |
| 2 | +Stooge Sort - a recursive sorting algorithm. |
| 3 | +
|
| 4 | +It is notably slow (worse than bubble sort) but is included here |
| 5 | +for educational purposes to illustrate recursive divide-and-conquer thinking. |
| 6 | +
|
| 7 | +Time Complexity: O(n^(log3/log1.5)) ≈ O(n^2.71) |
| 8 | +Space Complexity: O(log n) due to recursion stack |
| 9 | +
|
| 10 | +Reference: https://en.wikipedia.org/wiki/Stooge_sort |
| 11 | +""" |
| 12 | + |
| 13 | + |
| 14 | +def stooge_sort(arr: list[int], i: int = 0, j: int = -1) -> list[int]: |
| 15 | + """ |
| 16 | + Sorts a list in-place using the stooge sort algorithm and returns it. |
| 17 | +
|
| 18 | + >>> stooge_sort([3, 1, 2]) |
| 19 | + [1, 2, 3] |
| 20 | + >>> stooge_sort([5, 4, 3, 2, 1]) |
| 21 | + [1, 2, 3, 4, 5] |
| 22 | + >>> stooge_sort([1]) |
| 23 | + [1] |
| 24 | + >>> stooge_sort([]) |
| 25 | + [] |
| 26 | + >>> stooge_sort([2, 2, 1]) |
| 27 | + [1, 2, 2] |
| 28 | + >>> stooge_sort([10, -1, 5, 0]) |
| 29 | + [-1, 0, 5, 10] |
| 30 | + """ |
| 31 | + if len(arr) <= 1: |
| 32 | + return arr |
| 33 | + |
| 34 | + if j == -1: |
| 35 | + j = len(arr) - 1 |
| 36 | + |
| 37 | + if arr[i] > arr[j]: |
| 38 | + arr[i], arr[j] = arr[j], arr[i] |
| 39 | + |
| 40 | + if (j - i + 1) > 2: |
| 41 | + t = (j - i + 1) // 3 |
| 42 | + stooge_sort(arr, i, j - t) |
| 43 | + stooge_sort(arr, i + t, j) |
| 44 | + stooge_sort(arr, i, j - t) |
| 45 | + |
| 46 | + return arr |
| 47 | + |
| 48 | + |
| 49 | +if __name__ == "__main__": |
| 50 | + import doctest |
| 51 | + |
| 52 | + doctest.testmod() |
| 53 | + user_input = input("Enter numbers separated by commas: ") |
| 54 | + nums = [int(x.strip()) for x in user_input.split(",")] |
| 55 | + print(f"Sorted: {stooge_sort(nums)}") |
0 commit comments