The Slowest Sorting Algorithms

30 Jun, 2022
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.
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++
Java
Python3
C#
Javascript
// C++ program for the stooge sort#include <iostream>using namespace std; // Function to implement stooge sortvoid 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 Codeint 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 program for the// stooge sortclass GFG{     // Function to implement// stooge sortstatic 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 Codepublic 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 program for the stooge sort # Function to implement stooge sortdef 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 Codearr = [ 2, 4, 5, 3, 1 ]N = len(arr)  # Function Callstoogesort(arr, 0, N - 1) # Display the sorted arrayfor i in range(N):    print(arr[i], end = " ") # This code is contributed by code_hunt
// C# program for the// stooge sortusing System;class GFG{     // Function to implement// stooge sortstatic 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 Codepublic 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
<script> // Javascript program for the// stooge sort // Function to implement// stooge sortfunction 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 Codelet arr = [ 2, 4, 5, 3, 1 ];let N = arr.length; // Function Callstoogesort(arr, 0, N - 1); // Display the sorted arrayfor (let i = 0; i < N; i++){    document.write(arr[i] + " ");} // This code is contributed by avanitrachhadiya2155 </script>
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:
Find the maximum of the array and place it at the end of the array byRecursively call slowsort function for the maximum of the first N/2 elements.Recursively call slowsort function for the maximum of the remaining N/2 elements.Find the largest of that two maximum and store it at the end.Recursively call slowsort function for the entire array except for the maximum.Print the sorted array.
Find the maximum of the array and place it at the end of the array byRecursively call slowsort function for the maximum of the first N/2 elements.Recursively call slowsort function for the maximum of the remaining N/2 elements.Find the largest of that two maximum and store it at the end.Recursively call slowsort function for the entire array except for the maximum.
Recursively call slowsort function for the maximum of the first N/2 elements.Recursively call slowsort function for the maximum of the remaining N/2 elements.Find the largest of that two maximum and store it at the end.Recursively call slowsort function for the entire array except for the maximum.
Recursively call slowsort function for the maximum of the first N/2 elements.
Recursively call slowsort function for the maximum of the remaining N/2 elements.
Find the largest of that two maximum and store it at the end.
Recursively call slowsort function for the entire array except for the maximum.
Print the sorted array.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program to implement Slow sort#include <bits/stdc++.h>using namespace std; // Function for swap two numbers using// pointersvoid swap(int* xp, int* yp){    int temp = *xp;    *xp = *yp;    *yp = temp;} // Function that implements Slow Sortvoid 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 arrayvoid printArray(int arr[], int size){    int i;    for (i = 0; i < size; i++)        cout << arr[i] << " ";    cout << endl;} // Driver Codeint 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 program to implement Slow sortimport java.util.*; class GFG{ // Function that implements Slow Sortstatic 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 arraystatic void printArray(int arr[], int size){    int i;    for (i = 0; i < size; i++)        System.out.print(arr[i]+ " ");    System.out.println();} // Driver Codepublic 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
# Python program to implement Slow sort # Function that implements Slow Sortdef 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 print the arraydef printArray(arr, size):    i = 0;    for i in range(size):        print(arr[i], end=" ");    print(); # Driver Codeif __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# program to implement Slow sortusing System;class GFG{ // Function that implements Slow Sortstatic 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 arraystatic void printArray(int []arr, int size){    int i;    for (i = 0; i < size; i++)        Console.Write(arr[i] + " ");    Console.WriteLine();} // Driver Codepublic 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
<script> //Javascript program to implement Slow sort // Function that implements Slow Sortfunction 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 arrayfunction 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 callslowSort(arr, 0, N - 1); // Display the sorted arrayprintArray(arr, N); //This code is contributed by SoumikMondal</script>
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:
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.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.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.
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.
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.
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 valuesvector<int> A; // Function for print the arrayvoid printArray(vector<int> arr, int size){    int i;    for (i = 0; i < size; i++) {        cout << arr[i] << " ";    }} // The instruction set for a threadvoid 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 sortvoid 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 Codeint 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}
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 bogosortvoid 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 Codeint 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;}
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++
Python3
// C++ program to implement Bogo Sort// using random shuffle#include <bits/stdc++.h>using namespace std; // Function to check if array is// sorted or notbool 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 permutation// of the arrayvoid shuffle(int a[], int N){    for (int i = 0; i < N; i++)        swap(a[i], a[rand() % N]);} // Function to sort array using// Bogo sortvoid 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 arrayvoid printArray(int a[], int N){    for (int i = 0; i < N; i++) {         printf("%d ", a[i]);    }    printf("\n");} // Driver Codeint 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;}
# Python program to implement Bogo Sort# using random shuffleimport random # Function to check if array is# sorted or notdef isSorted(a,N):    while(N > 1):        N = N - 1                 # Break condition for        # unsorted array        if (a[N] < a[N - 1]):            return False    return True # To generate permutation# of the arraydef shuffle(a, N):    for i in range (0, N):        r = random.randint(0,N-1)        a[i], a[r] = a[r], a[i] # Function to sort array using# Bogo sortdef bogosort(a, N):    # If array is not sorted    # then shuffle array again    while (not isSorted(a, N)):        shuffle(a, N) # Function to print the arraydef printArray(a, N):    for i in range(N):        print(a[i], end=" ")    print()     # Driver code to test abovea = [3, 2, 5, 1, 0, 4]N=len(a) # Function Callbogosort(a,N)print("Array after sorting:",end="")printArray(a, N) # This code is contributed by Pushpesh Raj.
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.
ukasp
princi singh
code_hunt
29AjayKumar
GauravRajput1
avanitrachhadiya2155
SoumikMondal
akshaysingh98088
ankita_saini
pushpeshrajdx01
Algorithms-Sorting-Quiz
Algorithms
Analysis
Arrays
Articles
Sorting
Arrays
Sorting
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
DSA Sheet by Love Babbar
SDE SHEET - A Complete Guide for SDE Preparation
What is Hashing | A Complete Tutorial
Understanding Time Complexity with Simple Examples
Find if there is a path between two vertices in an undirected graph
Understanding Time Complexity with Simple Examples
Time Complexity and Space Complexity
Practice Questions on Time Complexity Analysis
Analysis of Algorithms | Set 2 (Worst, Average and Best Cases)
Types of Complexity Classes | P, NP, CoNP, NP hard and NP complete