Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions stooge_sort.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
# Python program to implement stooge sort

Python program to implement stooge sort
def stoogesort(arr, l, h):
if l >= h:
return

# If first element is smaller
# than last, swap them
If first element is smaller
than last, swap them
if arr[l]>arr[h]:
t = arr[l]
arr[l] = arr[h]
arr[h] = t

# If there are more than 2 elements in
# the array
If there are more than 2 elements in
the array
if h-l + 1 > 2:
t = (int)((h-l + 1)/3)

# Recursively sort first 2 / 3 elements
Recursively sort first 2 / 3 elements
stoogesort(arr, l, (h-t))

# Recursively sort last 2 / 3 elements
Recursively sort last 2 / 3 elements
stoogesort(arr, l + t, (h))

# Recursively sort first 2 / 3 elements
# again to confirm
Recursively sort first 2 / 3 elements
again to confirm
stoogesort(arr, l, (h-t))


# deriver
deriver
arr = [2, 4, 5, 3, 1]
n = len(arr)

Expand Down