Skip to content

Commit 2375bfb

Browse files
Charitoccclauss
authored andcommitted
Adding stooge sort (#1206)
* Adding stooge sort * Updated doctest * Just added underscore in the name
1 parent 6ac7b13 commit 2375bfb

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Diff for: sorts/stooge_sort.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
def stooge_sort(arr):
2+
"""
3+
>>> arr = [2, 4, 5, 3, 1]
4+
>>> stooge_sort(arr)
5+
>>> print(arr)
6+
[1, 2, 3, 4, 5]
7+
"""
8+
stooge(arr,0,len(arr)-1)
9+
10+
11+
def stooge(arr, i, h):
12+
13+
14+
if i >= h:
15+
return
16+
17+
# If first element is smaller than the last then swap them
18+
if arr[i]>arr[h]:
19+
arr[i], arr[h] = arr[h], arr[i]
20+
21+
# If there are more than 2 elements in the array
22+
if h-i+1 > 2:
23+
t = (int)((h-i+1)/3)
24+
25+
# Recursively sort first 2/3 elements
26+
stooge(arr, i, (h-t))
27+
28+
# Recursively sort last 2/3 elements
29+
stooge(arr, i+t, (h))
30+
31+
# Recursively sort first 2/3 elements
32+
stooge(arr, i, (h-t))
33+
34+

0 commit comments

Comments
 (0)