nrk009

Quick_Sort

Jul 27th, 2020
1,360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. /*
  2.     Was it all worth it ??
  3.     In the end all it remains are just memories!!!
  4.    
  5. */
  6.  
  7. #include<bits/stdc++.h>
  8. using namespace std;
  9.  
  10. #define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
  11. #define ll long long
  12. #define ld long double
  13. #define pb push_back
  14. #define fe first
  15. #define se second
  16. #define nl "\n"
  17. #define pp pair < ll , ll >
  18. #define sz(x) (ll)x.size()
  19. #define st(x) sort(x.begin(),x.end())
  20. #define rst(x) sort(x.rbegin(), x.rend())
  21. #define all(x) x.begin(),x.end()
  22. long double pi = 3.14159265358979323;
  23.  
  24. const double EPS = 1e-12;
  25. const int N = 1e6 + 5;
  26. //Just A Gentle Reminder about MOD
  27. const int mod = 1e9 + 7;
  28.  
  29. //Quick Sort
  30.  
  31. void print(ll n , ll arr[]){
  32.     for(int i = 0 ; i < n ; i++ )cout << arr[i] << " ";
  33. }
  34. ll partiton(ll arr[] , ll low , ll high){
  35.         int i = low - 1;
  36.         ll pivot = arr[high];
  37.         for(int j = low ; j <= high ; j++ ){
  38.                 if(arr[j] < pivot){
  39.                     i++ ;
  40.                     swap(arr[j] , arr[i]);
  41.                 }
  42.         }
  43.         swap(arr[i+1] , arr[high]);
  44.         return (i+1);
  45. }
  46.  
  47. void Quick_Sort(ll arr[] , ll low , ll high){
  48.  
  49.         if(low < high){
  50.             int pi = partiton(arr , low , high);
  51.             Quick_Sort(arr , low , pi - 1);
  52.             Quick_Sort(arr , pi + 1 , high);
  53.         }
  54.         //Best Case - O(nlogn)
  55.         //Worst cas - O(n^2) - when array is already sorted
  56.         //Memory - Stack Memory O(logn)
  57. }
  58.  
  59.  
  60.  
  61. int main()
  62. {
  63.     fast;
  64.     ll n;
  65.     cin >> n ;
  66.     ll arr[n];
  67.     for(int i = 0 ; i < n ;i++ ){
  68.         cin >> arr[i];            
  69.     }    
  70.  
  71.     Quick_Sort(arr , 0 , n - 1);
  72.     cout << "Quick_Sort\n";
  73.     print(n , arr);
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.    
  96.     return 0;
  97. //It's not end it yet
  98. }
Advertisement
Add Comment
Please, Sign In to add comment