Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update stooge_sort.py #145

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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