Advertisement
fireLUFFY

recursive-sorting

Oct 1st, 2021
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long
  5. #define inf 2e18
  6. int MOD=1000000007;//998244353;
  7. const int nn=1000050;
  8. bool prime[nn];    //array to store precalculated primes till 10^6
  9. void cal_primes(){memset(prime,true,sizeof(prime)); for(int i=2;i<=sqrt(nn);++i){ if(prime[i]==true){ for(int j=i*i;j<=nn;j+=i){prime[j]=false;}}}}
  10.  
  11. void shift(vector<int>& arr,int n)
  12. {
  13.     int l=arr[n-1],pos=(n-2);  
  14.     //this while loop, keeps on moving/shifting elements to one place towards the right,
  15.     //until the correct position for the last element is found. Then inserts into that place.
  16.     //can be done with repeated swapping too.
  17.     while(pos>=0 && arr[pos]>l)
  18.     {
  19.         arr[pos+1]=arr[pos];
  20.         pos--;
  21.     }
  22.     arr[pos+1]=l;
  23. }
  24.  
  25. void recbsort(vector<int>& arr,int n)
  26. {
  27.     //base case: 1 element, we don't need to do anything
  28.     if(n<=1)
  29.         return;
  30.  
  31.     //this loop, puts the largest element of current considered array in the last position.
  32.     //so basically, it sorts from back, that is puts, largest elements in their proper place
  33.     //for the current array/subarray
  34.     for(int i=0;i<n-1;i++)
  35.     {
  36.         int temp;
  37.         if(arr[i+1]<arr[i])
  38.         {
  39.             temp=arr[i];
  40.             arr[i]=arr[i+1];
  41.             arr[i+1]=temp;
  42.         }
  43.     }
  44.  
  45.     recbsort(arr,n-1);
  46. }
  47.  
  48. void recsort(vector<int>& arr, int n)
  49. {
  50.     //base case: 1 element, we don't need to do anything
  51.     if(n<=1)
  52.         return;
  53.  
  54.     recsort(arr,n-1);
  55.     shift(arr,n);   //insert the lase element in its correct position
  56. }
  57.  
  58. void solve(int t)
  59. {
  60.     int testcases=t;
  61.     while(t--)
  62.     {
  63.         int n;cin>>n;
  64.         vector<int>arr(n);
  65.         for(int i=0;i<n;i++)
  66.             cin>>arr[i];
  67.  
  68.         cout<<"initial array: ";
  69.         for(auto it:arr)
  70.             cout<<it<<" ";
  71.         cout<<endl;
  72.  
  73.         recsort(arr,n);     //recursive insertion sort
  74.         recbsort(arr,n);    //recursive bubble sort
  75.  
  76.         cout<<"sorted array: ";
  77.         for(auto it:arr)
  78.             cout<<it<<" ";
  79.     }
  80. }
  81.  
  82. main()
  83. {
  84.     auto start=chrono::system_clock::now();
  85.     {
  86.         ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  87.         int t=1;
  88.     //  cin>>t;
  89.         solve(t);
  90.     }
  91.     auto end=chrono::system_clock::now();
  92.     chrono::duration<double> elapsed=end-start;
  93. //  cout<<endl<<"Time taken: "<<elapsed.count()<<" sec";
  94.     return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement