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 firstname.lastname@example.org. 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
- Sort elements by frequency | Set 1
- Count Inversions in an array | Set 1 (Using Merge Sort)
- Merge Sort for Linked Lists
- Sort an array of 0s, 1s and 2s
- std::sort() in C++ STL
- Sort a nearly sorted (or K sorted) array
- Sort numbers stored on different machines
- Iterative Quick Sort
- Sort a linked list of 0s, 1s and 2s
- Counting Sort
- Sort elements by frequency | Set 2
- Radix Sort
- Sort n numbers in range from 0 to n^2 - 1 in linear time
- Bucket Sort
- Sort an array according to the order defined by another array
Improved By : Sam007