In this program, we are going to share a C++ program to perform stooge sort. If you are a beginner and want to start learning the C++ programming, then keep your close attention in this tutorial as I am going to share a program for C++ program to perform stooge sort with the output.
Copy the below C++ program and execute it with the help of GCC compiler. At the end of this program, We have shared the output of this program.
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 |
#include<iostream> using namespace std; void StoogeSort(int a[],int start, int end) { int temp; 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); } 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); cout<<"\nSorted Data "; for (i = 0; i < n; i++) cout<<"->"<<arr[i]; return 0; } |
Enter the number of data element to be sorted: 10
Enter element 1: 9
Enter element 2: 3
Enter element 3: 4
Enter element 4: 6
Enter element 5: 8
Enter element 6: 5
Enter element 7: 1
Enter element 8: 2
Enter element 9: 7
Enter element 10: 0
Sorted Data ->0->1->2->3->4->5->6->7->8->9
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.