forked from AllAlgorithms/cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstooge_sort.cpp
More file actions
48 lines (40 loc) · 831 Bytes
/
stooge_sort.cpp
File metadata and controls
48 lines (40 loc) · 831 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#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;
}