SHARE
TWEET

Untitled

a guest Nov 19th, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <ctime>
  3. #include <random>
  4.  
  5. using namespace std;
  6.  
  7. void QuickSort(int a[],int l, int r)
  8. {
  9.     int i,j;
  10.     i = l;
  11.     j = r;
  12.  
  13.     int mid = a[(i+j)/2];
  14.  
  15.     do
  16.     {
  17.         while(mid > a[i]) i++;
  18.         while(mid < a[j]) j--;
  19.  
  20.         if(i <= j)
  21.         {
  22.             swap(a[i],a[j]);
  23.             i++;
  24.             j--;  
  25.         }
  26.     } while (i < j);
  27.    
  28.     if(i < r) QuickSort(a,i,r);
  29.     if(j > l) QuickSort(a,l,j);
  30. }
  31. void PrintArr(const int a[], int n)
  32. {
  33.     for (int i(0); i < n; i++) {
  34.         cout << ((i!=0) and (i%5==0)? "\n": "");
  35.         cout << "A[" << i << "] = " << a[i]  << "  ";
  36.     }
  37. }
  38.  
  39. int main()
  40. {
  41.     const int N = 50;
  42.     int a[N];
  43.     srand(time(NULL));
  44.  
  45.     for(int i(0);i<N;i++)
  46.         a[i] = rand()%100+1;
  47.  
  48.     cout << "Array before\n";
  49.     PrintArr(a,N);
  50.  
  51.     double t1 = clock();
  52.     QuickSort(a,0,N-1);
  53.     double t2 = clock() - t1;
  54.    
  55.     cout << "\nArray after\n";
  56.     PrintArr(a,N);
  57.     cout << "\n Time to rearrange = " << t2/CLOCKS_PER_SEC << endl;
  58.     return 0;
  59. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top