From 21820acd1d473c7709a640e0fc323ca9e92d611a Mon Sep 17 00:00:00 2001 From: Charitoc <37042130+Charitoc@users.noreply.github.com> Date: Wed, 25 Sep 2019 15:23:53 +0300 Subject: [PATCH 1/3] Adding stooge sort --- sorts/stooge_sort.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 sorts/stooge_sort.py diff --git a/sorts/stooge_sort.py b/sorts/stooge_sort.py new file mode 100644 index 000000000000..9c65e4f56037 --- /dev/null +++ b/sorts/stooge_sort.py @@ -0,0 +1,37 @@ +def stoogesort(arr): + stooge(arr,0,len(arr)-1) + +def stooge(arr, i, h): + + """ + +>>> arr = [2, 4, 5, 3, 1] +>>> stoogesort(arr) +>>> print(arr) +[1, 2, 3, 4, 5] + +""" + + + + if i >= h: + return + + # If first element is smaller than the last then swap them + if arr[i]>arr[h]: + 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)) + + From 798a2460dca6108c707a3aa48711d0a0744d29ba Mon Sep 17 00:00:00 2001 From: Charitoc <37042130+Charitoc@users.noreply.github.com> Date: Thu, 26 Sep 2019 15:58:21 +0300 Subject: [PATCH 2/3] Updated doctest --- sorts/stooge_sort.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/sorts/stooge_sort.py b/sorts/stooge_sort.py index 9c65e4f56037..500abe574c73 100644 --- a/sorts/stooge_sort.py +++ b/sorts/stooge_sort.py @@ -1,19 +1,16 @@ def stoogesort(arr): + """ + >>> arr = [2, 4, 5, 3, 1] + >>> stoogesort(arr) + >>> print(arr) + [1, 2, 3, 4, 5] + """ stooge(arr,0,len(arr)-1) + def stooge(arr, i, h): - """ - ->>> arr = [2, 4, 5, 3, 1] ->>> stoogesort(arr) ->>> print(arr) -[1, 2, 3, 4, 5] - -""" - - if i >= h: return From 4629c768f36c01ec48de9a7a111bdd208e1579c0 Mon Sep 17 00:00:00 2001 From: Charitoc <37042130+Charitoc@users.noreply.github.com> Date: Thu, 26 Sep 2019 16:13:35 +0300 Subject: [PATCH 3/3] Just added underscore in the name --- sorts/stooge_sort.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sorts/stooge_sort.py b/sorts/stooge_sort.py index 500abe574c73..d2325abc9b38 100644 --- a/sorts/stooge_sort.py +++ b/sorts/stooge_sort.py @@ -1,7 +1,7 @@ -def stoogesort(arr): +def stooge_sort(arr): """ >>> arr = [2, 4, 5, 3, 1] - >>> stoogesort(arr) + >>> stooge_sort(arr) >>> print(arr) [1, 2, 3, 4, 5] """