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.
The key idea is that sorting the overlapping part twice exchanges the elements between the other two sections accordingly.
Approach:
Step 1: If value at index 0 is greater than value at last index, swap them.
Step 2: Recursively,
- Stooge sort the initial 2/3rd of the array.
- Stooge sort the last 2/3rd of the array.
- Stooge sort the initial 2/3rd again to confirm.
NOTE: Always take the ceil of ((2/3)*N) for selecting elements.
Illustration:
Lets consider an example: arr[] = {2, 4, 5, 3, 1}
- Step1: Initially, First and last elements are compared and if last is greater than first then they are swapped.
- Step2: Now, recursively sort initial 2/3rd of the elements as shown below:
- Step3: Then, recursively sort last 2/3rd of the elements, as shown below:
- Step4: Again, sort the initial 2/3rd of the elements to confirm final data is sorted.

Below is the implementation for the above approach:
C++
// C++ code to implement stooge sort
#include <iostream>
using namespace std;
// Function to implement stooge sort
void stoogesort(int arr[], int l, int h)
{
if (l >= h)
return;
// If first element is smaller than last,
// swap them
if (arr[l] > arr[h])
swap(arr[l], arr[h]);
// If there are more than 2 elements in
// the array
if (h - l + 1 > 2) {
int t = (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 Code
int main()
{
int arr[] = { 2, 4, 5, 3, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
// Calling Stooge Sort function to sort
// the array
stoogesort(arr, 0, n - 1);
// Display the sorted array
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
Java
// Java program to implement stooge sort
import java.io.*;
public class stooge {
// Function to implement stooge sort
static void stoogesort(int arr[], int l, int h)
{
if (l >= h)
return;
// If first element is smaller
// than last, swap them
if (arr[l] > arr[h]) {
int 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) {
int t = (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 Code
public static void main(String args[])
{
int arr[] = { 2, 4, 5, 3, 1 };
int n = arr.length;
stoogesort(arr, 0, n - 1);
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
}
// Code Contributed by Mohit Gupta_OMG <(0_o)>
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))
# deriver
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)>
C#
// C# program to implement stooge sort
using System;
class GFG {
// Function to implement stooge sort
static void stoogesort(int[] arr,
int l, int h)
{
if (l >= h)
return;
// If first element is smaller
// than last, swap them
if (arr[l] > arr[h]) {
int 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) {
int t = (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 Code
public static void Main()
{
int[] arr = { 2, 4, 5, 3, 1 };
int n = arr.Length;
// Calling Stooge Sort function
// to sort the array
stoogesort(arr, 0, n - 1);
// Display the sorted array
for (int i = 0; i < n; i++)
Console.Write(arr[i] + " ");
}
}
// This code is contributed by Sam007.
JavaScript
<script>
// Javascript program to implement stooge sort
// Function to implement stooge sort
function stoogesort(arr, l, h)
{
if (l >= h)
return;
// If first element is smaller
// than last, swap them
if (arr[l] > arr[h]) {
let 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) {
let t = parseInt((h - l + 1) / 3, 10);
// 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);
}
}
let arr = [ 2, 4, 5, 3, 1 ];
let n = arr.length;
// Calling Stooge Sort function
// to sort the array
stoogesort(arr, 0, n - 1);
// Display the sorted array
for (let i = 0; i < n; i++)
document.write(arr[i] + " ");
</script>
The running time complexity of stooge sort can be written as,
T(n) = 3T(2n/3) + ?(1)
Solution of above recurrence is O(n(log3/log1.5)) = O(n2.709), hence it is slower than even bubble sort(n^2).
Similar Reads
Sorting Algorithms A Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to Sorting Techniques â Data Structure and Algorithm Tutorials Sorting refers to rearrangement of a given array or list of elements according to a comparison operator on the elements. The comparison operator is used to decide the new order of elements in the respective data structure. Why Sorting Algorithms are ImportantThe sorting algorithm is important in Com
3 min read
Most Common Sorting Algorithms
Selection Sort Selection Sort is a comparison-based sorting algorithm. It sorts an array by repeatedly selecting the smallest (or largest) element from the unsorted portion and swapping it with the first unsorted element. This process continues until the entire array is sorted.First we find the smallest element an
8 min read
Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
12 min read
Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Heap Sort - Data Structures and Algorithms Tutorials Heap sort is a comparison-based sorting technique based on Binary Heap Data Structure. It can be seen as an optimization over selection sort where we first find the max (or min) element and swap it with the last (or first). We repeat the same process for the remaining elements. In Heap Sort, we use
14 min read
Counting Sort - Data Structures and Algorithms Tutorials Counting Sort is a non-comparison-based sorting algorithm. It is particularly efficient when the range of input values is small compared to the number of elements to be sorted. The basic idea behind Counting Sort is to count the frequency of each distinct element in the input array and use that info
9 min read