Open In App

Python Program for Stooge Sort

Last Updated : 28 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The Stooge sort is a recursive sorting algorithm. It is defined as below (for ascending order sorting).

Python Program for Stooge Sort

The provided Python code implements the Stooge Sort algorithm, a relatively inefficient sorting algorithm. It recursively divides the array into three parts and sorts the outer thirds to bring the largest element into the correct position. It starts with comparing the first and last elements, swapping them if necessary. Then, it recursively sorts the initial two-thirds and the last two-thirds, ensuring the middle part is in its final position. This process repeats recursively until the array is sorted. The driver code initializes an array, applies Stooge Sort, and prints the sorted array. Stooge Sort’s time complexity is relatively high, making it inefficient for practical use.

Step 1 : If value at index 0 is greater than
value at last index, swap them.
Step 2: Recursively,
a) Stooge sort the initial 2/3rd of the array.
b) Stooge sort the last 2/3rd of the array.
c) Stooge sort the initial 2/3rd again to confirm.

Python3




# 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 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 h-l+1 > 2:
  t = (int)((h-l+1)/3)
 
  # Recursively sort first 2/3 elements
  stoogesort(arr, l, (h-t))
 
  # Recursively sort last 2/3 elements
  stoogesort(arr, l+t, (h))
 
  # Recursively sort first 2/3 elements
  # again to confirm
  stoogesort(arr, l, (h-t))
 
 
# driver
arr = [2, 4, 5, 3, 1]
n = len(arr)
 
stoogesort(arr, 0, n-1)
 
for i in range(0, n):
 print(arr[i], end = ' ')
 
# Code Contributed by Mohit Gupta_OMG <(0_o)>


Output:

1 2 3 4 5 

Time Complexity:
The running time complexity of stooge sort can be written as,
T(n) = 3T(3n/2) + O(1)
Solution of above recurrence is O(n(log3/log1.5)) = O(n2.709), hence it is slower than even bubble sort(n2).
Auxiliary Space: O(n)

Please refer complete article on Stooge Sort for more details!



Previous Article
Next Article

Similar Reads

C++ Program for Stooge Sort
The Stooge sort is a recursive sorting algorithm. It is defined as below (for ascending order sorting). Step 1 : If value at index 0 is greater than value at last index, swap them. Step 2: Recursively, a) Stooge sort the initial 2/3rd of the array. b) Stooge sort the last 2/3rd of the array. c) Stooge sort the initial 2/3rd again to confirm. C/C++
2 min read
Java Program for Stooge Sort
The Stooge sort is a recursive sorting algorithm. It is defined as below (for ascending order sorting). Step 1 : If value at index 0 is greater than value at last index, swap them. Step 2: Recursively, a) Stooge sort the initial 2/3rd of the array. b) Stooge sort the last 2/3rd of the array. c) Stooge sort the initial 2/3rd again to confirm. Java C
2 min read
Stooge Sort
Stooge Sort is a recursive sorting algorithm. It is not much efficient but interesting sorting algorithm. It generally divides the array into two overlapping parts (2/3 each). After that it performs sorting in first 2/3 part and then it performs sorting in last 2/3 part. And then, sorting is done on first 2/3 part to ensure that the array is sorted
6 min read
Tag sort or Bucket sort or Bin sort in Python
Tag sort, also known as Bucket sort or Bin sort, is a non-comparison based sorting algorithm that distributes elements of an array into a number of "buckets", and then sorts each bucket individually. Tag sort or Bucket sort or Bin sort Algorithm:Determine Range:Find the maximum and minimum values in the input array to determine the range of tags ne
2 min read
Comparison among Bubble Sort, Selection Sort and Insertion Sort
Bubble Sort, Selection Sort, and Insertion Sort are simple sorting algorithms that are commonly used to sort small datasets or as building blocks for more complex sorting algorithms. Here's a comparison of the three algorithms: Bubble Sort:Time complexity: O(n^2) in the worst and average cases, O(n) in the best case (when the input array is already
15 min read
Python Program for Odd-Even Sort / Brick Sort
This is basically a variation of bubble-sort. This algorithm is divided into two phases- Odd and Even Phase. The algorithm runs until the array elements are sorted and in each iteration two phases occurs- Odd and Even Phases. In the odd phase, we perform a bubble sort on odd indexed elements and in the even phase, we perform a bubble sort on even i
2 min read
C/C++ Program for Odd-Even Sort / Brick Sort
This is basically a variation of bubble-sort. This algorithm is divided into two phases- Odd and Even Phase. The algorithm runs until the array elements are sorted and in each iteration two phases occurs- Odd and Even Phases. In the odd phase, we perform a bubble sort on odd indexed elements and in the even phase, we perform a bubble sort on even i
2 min read
Java Program for Odd-Even Sort / Brick Sort
This is basically a variation of bubble-sort. This algorithm is divided into two phases- Odd and Even Phase. The algorithm runs until the array elements are sorted and in each iteration two phases occurs- Odd and Even Phases. In the odd phase, we perform a bubble sort on odd indexed elements and in the even phase, we perform a bubble sort on even i
2 min read
Program to sort an array of strings using Selection Sort
Given an array of strings, sort the array using Selection Sort. Examples: Input : paper true soap floppy flower Output : floppy, flower, paper, soap, true Prerequisite : Selection Sort. C/C++ Code // C++ program to implement selection sort for // array of strings. #include &lt;bits/stdc++.h&gt; #include &lt;string.h&gt; using namespace std; #define
7 min read
Sort a Dictionary Without Using Sort Function in Python
As we all know Dictionaries in Python are unordered by default, which means they can’t be sorted directly. However, we can sort the keys or values of a dictionary and create a new sorted dictionary from the sorted keys or values. We can sort a list in a Dictionary using the inbuilt dictionary sort() function. But in this article, we will learn how
3 min read