File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change 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<<" \n Enter 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<<" \n Sorted Data " ;
44+ for (i = 0 ; i < n; i++)
45+ cout<<" ->" <<arr[i];
46+
47+ return 0 ;
48+ }
You can’t perform that action at this time.
0 commit comments