Skip to content
Related Articles

Related Articles

Improve Article
The Slowest Sorting Algorithms
  • Difficulty Level : Easy
  • Last Updated : 16 Jun, 2021

A Sorting Algorithm is used to rearrange a given array or list elements according to a comparison operator on the elements. The comparison operator is used to decide the new order of the element in the respective data structure. But Below is some of the slowest sorting algorithms:

Stooge Sort: A Stooge sort is a recursive sorting algorithm. It recursively divides and sorts the array in parts. Below are the steps of the Stooge Sort:

  • If the value at index 0 is greater than the value at the last index, swap them.
  • If the number of elements in the array is greater than two:
    • Recursively call stoogesort function for the initial 2/3rd elements of the array.
    • Recursively call stoogesort function for the last 2/3rd elements of the array.
    • Recursively call stoogesort function for the initial 2/3rd elements again to confirm the resultant array is sorted or not.
  • Print the sorted array.

Below is the implementation of the above approach:

C++




// C++ program for the stooge sort
#include <iostream>
using namespace std;
 
// Function to implement stooge sort
void stoogesort(int arr[], int l, int h)
{
    // Base Case
    if (l >= h)
        return;
 
    // If first element is smaller than
    // last element, 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 the first
        // 2/3 elements
        stoogesort(arr, l, h - t);
 
        // Recursively sort the last
        // 2/3 elements
        stoogesort(arr, l + t, h);
 
        // Recursively sort the first
        // 2/3 elements again
        stoogesort(arr, l, h - t);
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 4, 5, 3, 1 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    stoogesort(arr, 0, N - 1);
 
    // Display the sorted array
    for (int i = 0; i < N; i++) {
        cout << arr[i] << " ";
    }
 
    return 0;
}

Java




// Java program for the
// stooge sort
class GFG{
     
// Function to implement
// stooge sort
static void stoogesort(int arr[],
                       int l, int h)
{
  // Base Case
  if (l >= h)
    return;
 
  // If first element is smaller
  // than last element, swap them
  if (arr[l] > arr[h])
  {
    int temp = arr[l];
    arr[l] = arr[h];
    arr[h] = temp;
  }
 
  // If there are more than
  // 2 elements in the array
  if (h - l + 1 > 2)
  {
    int t = (h - l + 1) / 3;
 
    // Recursively sort the
    // first 2/3 elements
    stoogesort(arr, l, h - t);
 
    // Recursively sort the
    // last 2/3 elements
    stoogesort(arr, l + t, h);
 
    // Recursively sort the
    // first 2/3 elements again
    stoogesort(arr, l, h - t);
  }
}
 
// Driver Code
public static void main(String[] args)
{
  int arr[] = {2, 4, 5, 3, 1};
  int N = arr.length;
 
  // Function Call
  stoogesort(arr, 0, N - 1);
 
  // Display the sorted array
  for (int i = 0; i < N; i++)
  {
    System.out.print(arr[i] + " ");
  }
}
}
 
// This code is contributed by Chitranayal

Python3




# Python3 program for the stooge sort
 
# Function to implement stooge sort
def stoogesort(arr, l, h):
     
    # Base Case
    if (l >= h):
        return
  
    # If first element is smaller than
    # last element, swap them
    if (arr[l] > arr[h]):
        temp = arr[l]
        arr[l] = arr[h]
        arr[h] = temp
 
    # If there are more than 2 elements
    # in the array
    if (h - l + 1 > 2):
        t = (h - l + 1) // 3
  
        # Recursively sort the first
        # 2/3 elements
        stoogesort(arr, l, h - t)
  
        # Recursively sort the last
        # 2/3 elements
        stoogesort(arr, l + t, h)
  
        # Recursively sort the first
        # 2/3 elements again
        stoogesort(arr, l, h - t)
     
# Driver Code
arr = [ 2, 4, 5, 3, 1 ]
N = len(arr)
  
# Function Call
stoogesort(arr, 0, N - 1)
 
# Display the sorted array
for i in range(N):
    print(arr[i], end = " ")
 
# This code is contributed by code_hunt

C#




// C# program for the
// stooge sort
using System;
class GFG{
     
// Function to implement
// stooge sort
static void stoogesort(int []arr,
                       int l, int h)
{
  // Base Case
  if (l >= h)
    return;
 
  // If first element is smaller
  // than last element, swap them
  if (arr[l] > arr[h])
  {
    int temp = arr[l];
    arr[l] = arr[h];
    arr[h] = temp;
  }
 
  // If there are more than
  // 2 elements in the array
  if (h - l + 1 > 2)
  {
    int t = (h - l + 1) / 3;
 
    // Recursively sort the
    // first 2/3 elements
    stoogesort(arr, l, h - t);
 
    // Recursively sort the
    // last 2/3 elements
    stoogesort(arr, l + t, h);
 
    // Recursively sort the
    // first 2/3 elements again
    stoogesort(arr, l, h - t);
  }
}
 
// Driver Code
public static void Main(String[] args)
{
  int []arr = {2, 4, 5, 3, 1};
  int N = arr.Length;
 
  // Function Call
  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 Princi Singh

Javascript




<script>
 
// Javascript program for the
// stooge sort
 
// Function to implement
// stooge sort
function stoogesort(arr, l, h)
{
     
    // Base Case
    if (l >= h)
        return;
     
    // If first element is smaller
    // than last element, swap them
    if (arr[l] > arr[h])
    {
        let temp = arr[l];
        arr[l] = arr[h];
        arr[h] = temp;
    }
     
    // If there are more than
    // 2 elements in the array
    if (h - l + 1 > 2)
    {
        let t = Math.floor((h - l + 1) / 3);
         
        // Recursively sort the
        // first 2/3 elements
        stoogesort(arr, l, h - t);
         
        // Recursively sort the
        // last 2/3 elements
        stoogesort(arr, l + t, h);
         
        // Recursively sort the
        // first 2/3 elements again
        stoogesort(arr, l, h - t);
    }
}
 
// Driver Code
let arr = [ 2, 4, 5, 3, 1 ];
let N = arr.length;
 
// Function Call
stoogesort(arr, 0, N - 1);
 
// Display the sorted array
for (let i = 0; i < N; i++)
{
    document.write(arr[i] + " ");
}
 
// This code is contributed by avanitrachhadiya2155
 
</script>
Output: 
1 2 3 4 5

 

Time Complexity: O(N2.709). Therefore, it is slower than even the Bubble Sort that has a time complexity of O(N2).



Slow Sort: The slow sort is an example of Multiply And Surrender a tongue-in-cheek joke of divide and conquer. Slow sort stores the maximum element of the array at the last position by recursively divides the array by half and compares each of them. Then it recursively calls the array without the previous maximum element and stores the new maximum element at the new last position. Below are the steps of Slow sort:

  1. Find the maximum of the array and place it at the end of the array by
    1. Recursively call slowsort function for the maximum of the first N/2 elements.
    2. Recursively call slowsort function for the maximum of the remaining N/2 elements.
    3. Find the largest of that two maximum and store it at the end.
    4. Recursively call slowsort function for the entire array except for the maximum.
  2. Print the sorted array.

Below is the implementation of the above approach:

C++




// C++ program to implement Slow sort
#include <bits/stdc++.h>
using namespace std;
 
// Function for swap two numbers using
// pointers
void swap(int* xp, int* yp)
{
    int temp = *xp;
    *xp = *yp;
    *yp = temp;
}
 
// Function that implements Slow Sort
void slowSort(int A[], int i, int j)
{
    // Base Case
    if (i >= j)
        return;
 
    // Middle value
    int m = (i + j) / 2;
 
    // Recursively call with left half
    slowSort(A, i, m);
 
    // Recursively call with right half
    slowSort(A, m + 1, j);
 
    // Swap if first element
    // is lower than second
    if (A[j] < A[m]) {
        swap(&A[j], &A[m]);
    }
 
    // Recursively call with whole
    // array except maximum element
    slowSort(A, i, j - 1);
}
 
// Function to print the array
void printArray(int arr[], int size)
{
    int i;
    for (i = 0; i < size; i++)
        cout << arr[i] << " ";
    cout << endl;
}
 
// Driver Code
int main()
{
    int arr[] = { 6, 8, 9, 4, 12, 1 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    slowSort(arr, 0, N - 1);
 
    // Display the sorted array
    printArray(arr, N);
 
    return 0;
}

Java




// Java program to implement Slow sort
import java.util.*;
 
class GFG
{
 
// Function that implements Slow Sort
static void slowSort(int A[], int i, int j)
{
    // Base Case
    if (i >= j)
        return;
 
    // Middle value
    int m = (i + j) / 2;
 
    // Recursively call with left half
    slowSort(A, i, m);
 
    // Recursively call with right half
    slowSort(A, m + 1, j);
 
    // Swap if first element
    // is lower than second
    if (A[j] < A[m])
    {
        int temp = A[j];
        A[j] = A[m];
        A[m] = temp;
    }
 
    // Recursively call with whole
    // array except maximum element
    slowSort(A, i, j - 1);
}
 
// Function to print the array
static void printArray(int arr[], int size)
{
    int i;
    for (i = 0; i < size; i++)
        System.out.print(arr[i]+ " ");
    System.out.println();
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 6, 8, 9, 4, 12, 1 };
    int N = arr.length;
 
    // Function call
    slowSort(arr, 0, N - 1);
 
    // Display the sorted array
    printArray(arr, N);
}
}
 
// This code is contributed by 29AjayKumar

Python3




# Python program to implement Slow sort
 
# Function that implements Slow Sort
def slowSort(A, i, j):
   
    # Base Case
    if (i >= j):
        return;
 
    # Middle value
    m = (i + j) // 2;
 
    # Recursively call with left half
    slowSort(A, i, m);
 
    # Recursively call with right half
    slowSort(A, m + 1, j);
 
    # Swap if first element
    # is lower than second
    if (A[j] < A[m]):
        temp = A[j];
        A[j] = A[m];
        A[m] = temp;
 
    # Recursively call with whole
    # array except maximum element
    slowSort(A, i, j - 1);
 
# Function to prthe array
def printArray(arr, size):
    i = 0;
    for i in range(size):
        print(arr[i], end=" ");
    print();
 
# Driver Code
if __name__ == '__main__':
    arr = [6, 8, 9, 4, 12, 1];
    N = len(arr);
 
    # Function call
    slowSort(arr, 0, N - 1);
 
    # Display the sorted array
    printArray(arr, N);
 
    # This code contributed by gauravrajput1

C#




// C# program to implement Slow sort
using System;
class GFG
{
 
// Function that implements Slow Sort
static void slowSort(int []A, int i, int j)
{
    // Base Case
    if (i >= j)
        return;
 
    // Middle value
    int m = (i + j) / 2;
 
    // Recursively call with left half
    slowSort(A, i, m);
 
    // Recursively call with right half
    slowSort(A, m + 1, j);
 
    // Swap if first element
    // is lower than second
    if (A[j] < A[m])
    {
        int temp = A[j];
        A[j] = A[m];
        A[m] = temp;
    }
 
    // Recursively call with whole
    // array except maximum element
    slowSort(A, i, j - 1);
}
 
// Function to print the array
static void printArray(int []arr, int size)
{
    int i;
    for (i = 0; i < size; i++)
        Console.Write(arr[i] + " ");
    Console.WriteLine();
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 6, 8, 9, 4, 12, 1 };
    int N = arr.Length;
 
    // Function call
    slowSort(arr, 0, N - 1);
 
    // Display the sorted array
    printArray(arr, N);
}
}
 
// This code is contributed by 29AjayKumar

Javascript




<script>
 //Javascript program to implement Slow sort
 
// Function that implements Slow Sort
function slowSort(A, i,j)
{
    // Base Case
    if (i >= j)
        return;
 
    // Middle value
    var m = parseInt((i + j) / 2);
 
    // Recursively call with left half
    slowSort(A, i, m);
 
    // Recursively call with right half
    slowSort(A, m + 1, j);
 
    // Swap if first element
    // is lower than second
    if (A[j] < A[m]) {
        //swapp(A[j], A[m]);
        var t = A[j];
        A[j]=A[m];
        A[m]=t;
    }
     
    // Recursively call with whole
    // array except maximum element
    slowSort(A, i, j - 1);
 
}
 
// Function to print the array
function printArray(arr, size)
{
    var i;
    for (i = 0; i < size; i++)
        document.write( arr[i] + " ");
    document.write("<br>");
}
 
var arr = [ 6, 8, 9, 4, 12, 1 ];
var N = arr.length;
 
// Function call
slowSort(arr, 0, N - 1);
 
// Display the sorted array
printArray(arr, N);
 
//This code is contributed by SoumikMondal
</script>
Output: 
1 4 6 8 9 12

 

Time Complexity:

  • Base Case: O(N((log N)/(2+e)) where, e > 0
  • Average Case: O(N(log(N)/2))

Even the best case is worse than Bubble sort. It is less efficient than Stooge sort. 

Sleep Sort: Below is the steps of Stooge sort:

  1. Create different threads for each of the elements in the input array and then each thread sleeps for an amount of time which is proportional to the value of the corresponding array element.
  2. The thread having the least amount of sleeping time wakes up first and the number gets printed and then the second least element and so on.
  3. The largest element wakes up after a long time and then the element gets printed at the last. Thus, the output is a sorted one.

All this Multithreading process happens in the background and at the core of the OS

Below is the implementation of the above approach: 



C++




// C++ program to implement Sleep sort
#ifdef _WIN32
 
// sleep() function for windows machine
#include <Windows.h>
#else
 
// sleep() function for linux machine
#include <unistd.h>
#endif
#include <iostream>
#include <thread>
#include <vector>
 
using namespace std;
 
// Array for storing the sorted values
vector<int> A;
 
// Function for print the array
void printArray(vector<int> arr, int size)
{
    int i;
    for (i = 0; i < size; i++) {
        cout << arr[i] << " ";
    }
}
 
// The instruction set for a thread
void add(int x)
{
    // Temporarily suspend execution
    // of each thread for x amount
    // of seconds
    sleep(x);
 
    // Every thead will wake up after
    // a particular time and push the
    // value in sorted array
    A.push_back(x);
}
 
// Function for Sleep sort
void sleepSort(int arr[], int N)
{
 
    vector<thread> threads;
    for (int i = 0; i < N; i++) {
 
        // New threads were launched by
        // using function pointer as
        // callable
        threads.push_back(
            thread(add, arr[i]));
    }
 
    // Waiting for each thread
    // to finish execution
    for (auto& th : threads) {
        th.join();
    }
 
    // Display the sorted array
    cout << "Array after sorting: ";
    printArray(A, A.size());
}
 
// Driver Code
int main()
{
    int arr[] = { 8, 9, 1, 4, 3 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // sleep_sort function call
    sleepSort(arr, N);
 
    return 0;
}
 
// To run compile using -pthread
// {  1, 3, 4, 8, 9}
Output: 
Array after sorting 1 3 4 8 9

 

Time Complexity: O(max(input) + N) where, input = value of array element

Other algorithm’s time complexity depends upon the number of data but for sleep sort, it depends on the amount of data. This algorithm won’t work for negative numbers as a thread cannot sleep for a negative amount of time.

Bogo Sort: Two versions of this algorithm exist: one enumerates all permutations until it hits a sorted one, and a randomized version that randomly permutes its input.

Example 1:

C++




// C++ program to implement Bogo Sort
// using permutation
#include <bits/stdc++.h>
using namespace std;
 
// Function to sort array using bogosort
void bogosort(int arr[], int N)
{
    // Run the loop until
    // array is not sorted
    while (!is_sorted(arr, arr + N)) {
 
        // All possible permutations
        next_permutation(arr, arr + N);
    }
}
// Driver Code
int main()
{
 
    int arr[] = { 8, 9, 1, 4, 3 };
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    bogosort(arr, N);
 
    // Display the sorted array
    cout << "Array after sorting ";
    for (int i = 0; i < N; ++i) {
        cout << arr[i] << " ";
    }
    cout << endl;
 
    return 0;
}
Output: 
Array after sorting 1 3 4 8 9

 

Time Complexity:

  • Base Case: O(N)
  • Average Case: O(N!)
  • Worst Case: O(N!)

Example 2:

C++




// C++ program to implement Bogo Sort
// using random shuffle
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if array is
// sorted or not
bool isSorted(int a[], int N)
{
    while (--N > 1) {
 
        // Break condition for
        // unsorted array
        if (a[N] < a[N - 1])
            return false;
    }
    return true;
}
 
// Function to generate permuatation
// of the array
void shuffle(int a[], int N)
{
    for (int i = 0; i < N; i++)
        swap(a[i], a[rand() % N]);
}
 
// Function to sort array using
// Bogo sort
void bogosort(int a[], int N)
{
    // If array is not sorted
    // then shuffle array again
    while (!isSorted(a, N)) {
 
        shuffle(a, N);
    }
}
 
// Function to print the array
void printArray(int a[], int N)
{
    for (int i = 0; i < N; i++) {
 
        printf("%d ", a[i]);
    }
    printf("\n");
}
 
// Driver Code
int main()
{
    int a[] = { 3, 2, 5, 1, 0, 4 };
    int N = sizeof a / sizeof a[0];
 
    // Function Call
    bogosort(a, N);
    printf("Array after sorting:");
    printArray(a, N);
    return 0;
}
Output: 
Array after sorting:0 1 2 3 4 5

 

Time Complexity:

  • Base Case: O(N)
  • Average Case: O(N*N!)
  • Worst Case: O(∞)

Clearly, in the worst situation, Bogo sort using random shuffle takes an infinite amount of time to sort an array, and we may say that this is the slowest sorting algorithm. But the thing about Bogo Sort is that it violates some rules in Complexity Analysis. One of the rules is that you actually have to progress towards a goal. You can’t just obviously waste time for example by putting delay loops. The Slow Sort or stooge sort algorithm actually never makes a wrong move. Once it swaps two nodes the nodes will be in the correct order relative to each other and their order will not be reversed.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live 

My Personal Notes arrow_drop_up
Recommended Articles
Page :