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
- 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?
- Insertion sort to sort even and odd positioned elements in different orders
- Program to sort an array of strings using Selection Sort
- Bucket Sort To Sort an Array with Negative Numbers
- Java Program for Odd-Even Sort / Brick Sort
- Serial Sort v/s Parallel Sort in Java
- Odd Even Transposition Sort / Brick Sort using pthreads
- Quick Sort vs Merge Sort
- C/C++ Program for Odd-Even Sort / Brick Sort
- Odd-Even Sort / Brick Sort
- Sort all even numbers in ascending order and then sort all odd numbers in descending order
Improved By : Sam007