Skip to content

Commit 55bea59

Browse files
committed
added stooge sort.cpp
1 parent 7fa3db7 commit 55bea59

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

sorting/stooge_sort.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include<iostream>
2+
3+
using namespace std;
4+
5+
// A function implementing stooge sort.
6+
void StoogeSort(int a[],int start, int end)
7+
{
8+
int temp;
9+
// Further breaking the array if the Subpart's length is more than 2.
10+
if(end-start+1 > 2)
11+
{
12+
temp = (end-start+1)/3;
13+
StoogeSort(a, start, end-temp);
14+
StoogeSort(a, start+temp, end);
15+
StoogeSort(a, start, end-temp);
16+
}
17+
18+
// swapping the element at start and end.
19+
if(a[end] < a[start])
20+
{
21+
temp = a[start];
22+
a[start] = a[end];
23+
a[end] = temp;
24+
}
25+
}
26+
27+
int main()
28+
{
29+
int n, i;
30+
cout<<"\nEnter the number of data element to be sorted: ";
31+
cin>>n;
32+
33+
int arr[n];
34+
for(i = 0; i < n; i++)
35+
{
36+
cout<<"Enter element "<<i+1<<": ";
37+
cin>>arr[i];
38+
}
39+
40+
StoogeSort(arr, 0, n-1);
41+
42+
// Printing the sorted data.
43+
cout<<"\nSorted Data ";
44+
for (i = 0; i < n; i++)
45+
cout<<"->"<<arr[i];
46+
47+
return 0;
48+
}

0 commit comments

Comments
 (0)