-
-
Notifications
You must be signed in to change notification settings - Fork 51.1k
Expand file tree
/
Copy pathstooge_sort.py
More file actions
57 lines (40 loc) · 1.41 KB
/
Copy pathstooge_sort.py
File metadata and controls
57 lines (40 loc) · 1.41 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
from typing import Protocol
class Comparable(Protocol):
def __lt__(self, other: object, /) -> bool: ...
def stooge_sort[T: Comparable](arr: list[T]) -> list[T]:
"""
Examples:
>>> stooge_sort([18.1, 0, -7.1, -1, 2, 2])
[-7.1, -1, 0, 2, 2, 18.1]
>>> stooge_sort([])
[]
>>> stooge_sort(["c", "a", "b"])
['a', 'b', 'c']
>>> stooge_sort([2.5, -1, 0.0])
[-1, 0.0, 2.5]
>>> stooge_sort([1, "a"])
Traceback (most recent call last):
...
TypeError: '<' not supported between instances of 'str' and 'int'
"""
stooge(arr, 0, len(arr) - 1)
return arr
def stooge[T: Comparable](arr: list[T], i: int, h: int) -> None:
if i >= h:
return
# If first element is smaller than the last then swap them
if arr[h] < arr[i]:
arr[i], arr[h] = arr[h], arr[i]
# If there are more than 2 elements in the array
if h - i + 1 > 2:
t = (int)((h - i + 1) / 3)
# Recursively sort first 2/3 elements
stooge(arr, i, (h - t))
# Recursively sort last 2/3 elements
stooge(arr, i + t, (h))
# Recursively sort first 2/3 elements
stooge(arr, i, (h - t))
if __name__ == "__main__":
user_input = input("Enter numbers separated by a comma:\n").strip()
unsorted = [int(item) for item in user_input.split(",")]
print(stooge_sort(unsorted))