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.
NOTE: Always take the ceil of ((2/3)*N) for selecting elements.
Input : 2 4 5 3 1 Output : 1 2 3 4 5 Explanation: Initially, swap 2 and 1 following above step 1. 1 4 5 3 2 Now, recursively sort initial 2/3rd of the elements. 1 4 5 3 2 1 3 4 5 2 Then, recursively sort last 2/3rd of the elements. 1 3 4 5 2 1 2 3 4 5 Again, sort the initial 2/3rd of the elements to confirm final data is sorted. 1 2 3 4 5
1 2 3 4 5
The running time complexity of stooge sort can be written as,
T(n) = 3T(3n/2) + ?(1)
Solution of above recurrence is O(n(log3/log1.5)) = O(n2.709), hence it is slower than even bubble sort(n^2).
This article is contributed by DANISH KALEEM. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.rf
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.
- C++ Program for Stooge Sort
- Java Program for Stooge Sort
- Python Program for Stooge Sort
- Comparison among Bubble Sort, Selection Sort and Insertion Sort
- Why Quick Sort preferred for Arrays and Merge Sort for Linked Lists?
- Odd-Even Sort / Brick Sort
- Bucket Sort To Sort an Array with Negative Numbers
- Serial Sort v/s Parallel Sort in Java
- Program to sort an array of strings using Selection Sort
- C/C++ Program for Odd-Even Sort / Brick Sort
- Java Program for Odd-Even Sort / Brick Sort
- Insertion sort to sort even and odd positioned elements in different orders
- Quick Sort vs Merge Sort
- Odd Even Transposition Sort / Brick Sort using pthreads
- Sort an Array which contain 1 to N values in O(N) using Cycle Sort
- Add elements in start to sort the array | Variation of Stalin Sort
- Merge Sort vs. Insertion Sort
- sort() vs. partial_sort() vs. nth_element() + sort() in C++ STL
- Sort all even numbers in ascending order and then sort all odd numbers in descending order
- Merge Sort for Linked Lists
Improved By : Sam007