forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstooge_sort.py
More file actions
35 lines (23 loc) · 726 Bytes
/
Copy pathstooge_sort.py
File metadata and controls
35 lines (23 loc) · 726 Bytes
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
# See what stooge sort dooes
# https://www.youtube.com/watch?v=vIDkfrSdID8
def stooge_sort_(arr, l, h):
if l >= h:
return 0
# If first element is smaller than last, then swap
if arr[l] > arr[h]:
t = arr[l]
arr[l] = arr[h]
arr[h] = t
# If there are more than 2 elements in array
if h - l + 1 > 2:
t = (int)((h - l + 1) / 3)
# Recursively sort first 2 / 3 elements
stooge_sort_(arr, l, (h - t))
# Recursively sort last 2 / 3 elements
stooge_sort_(arr, l + t, (h))
# Recursively sort first 2 / 3 elements
stooge_sort_(arr, l, (h - t))
arr = [2, 4, 5, 3, 1]
n = len(arr)
stooge_sort_(arr, 0, n - 1)
print(arr)