stooge sort Algorithm
The Stooge Sort algorithm is a simple, inefficient, and often considered a humorous sorting algorithm that is primarily used for educational purposes or as a demonstration of the basic principles of sorting. It is based on the "Divide and Conquer" approach, but instead of dividing the input into equal parts, it splits the input into three overlapping sections. The name "stooge sort" is inspired by the slapstick comedy trio "The Three Stooges," who were famous for their comical and chaotic antics, much like the nature of this algorithm.
The Stooge Sort algorithm works by recursively sorting the first two-thirds of the input data, followed by the last two-thirds, and finally the first two-thirds again. This process is repeated until the base case is reached, which is when there are only two elements left to compare and swap if necessary. The efficiency of the Stooge Sort algorithm is quite poor, with a time complexity of O(n^2.71), making it impractical for use in real-world applications. However, its simplicity and unusual approach to sorting make it an interesting subject for teaching and learning about the basic concepts of algorithms and recursion.
#include<iostream>
using namespace std;
// A function implementing stooge sort.
void StoogeSort(int a[],int start, int end)
{
int temp;
// Further breaking the array if the Subpart's length is more than 2.
if(end-start+1 > 2)
{
temp = (end-start+1)/3;
StoogeSort(a, start, end-temp);
StoogeSort(a, start+temp, end);
StoogeSort(a, start, end-temp);
}
// swapping the element at start and end.
if(a[end] < a[start])
{
temp = a[start];
a[start] = a[end];
a[end] = temp;
}
}
int main()
{
int n, i;
cout<<"\nEnter the number of data element to be sorted: ";
cin>>n;
int arr[n];
for(i = 0; i < n; i++)
{
cout<<"Enter element "<<i+1<<": ";
cin>>arr[i];
}
StoogeSort(arr, 0, n-1);
// Printing the sorted data.
cout<<"\nSorted Data ";
for (i = 0; i < n; i++)
cout<<"->"<<arr[i];
return 0;
}