stankovic96

[AINS] Stooge sort

Dec 17th, 2018
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. #include<iostream>
  2. #include<math.h>
  3. using namespace std;
  4.  
  5. int n=7,i;
  6. int arr[7]={8,1,6,2,4,2,5};
  7. int cnt=0;
  8.  
  9. int StoogeSort(int a[],int start,int end){
  10.   int temp,temp2;
  11.   cnt++;
  12.   cout<<cnt<<")=[ ";
  13.   for(i=0;i<n-1;i++){
  14.     cout<<arr[i]<<",";
  15.   }
  16.   cout<<arr[n-1];
  17.   cout<<"]"<<endl;
  18.  
  19. if(a[end]<a[start]){
  20.     temp=a[start];
  21.     a[start]=a[end];
  22.     a[end]=temp;
  23.   }
  24. if(start+1>=end)
  25.     return 0;
  26.   //if(end-start+1>2){
  27.  
  28.    // temp2=ceil((float)(end-start+1)/(float)3);
  29.     temp=floor((float)(end-start+1)/(float)3);
  30.  //   cout<<"ceil="<<temp<<"  floor="<<temp2<<endl;
  31.     StoogeSort(a,start,end-temp);
  32.     StoogeSort(a,start+temp,end);
  33.     StoogeSort(a,start,end-temp);
  34.  
  35.  
  36. }
  37. int main(){
  38.   //cout<<"Enter n=";
  39.   //cin>>n;
  40.  /* cout<<"Enter elements";
  41.   for(i=0;i<n;i++){
  42.     cin>>arr[i];
  43.   }
  44. */
  45.   StoogeSort(arr,0,n-1);
  46.   cout<<"Sorted DAta";
  47.   for(i=0;i<n;i++){
  48.     cout<<"->"<<arr[i];
  49.   }
  50.   return 0;
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment