Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- #include <random>
- using namespace std;
- void QuickSort(int a[],int l, int r)
- {
- int i,j;
- i = l;
- j = r;
- int mid = a[(i+j)/2];
- do
- {
- while(mid > a[i]) i++;
- while(mid < a[j]) j--;
- if(i <= j)
- {
- swap(a[i],a[j]);
- i++;
- j--;
- }
- } while (i < j);
- if(i < r) QuickSort(a,i,r);
- if(j > l) QuickSort(a,l,j);
- }
- void PrintArr(const int a[], int n)
- {
- for (int i(0); i < n; i++) {
- cout << ((i!=0) and (i%5==0)? "\n": "");
- cout << "A[" << i << "] = " << a[i] << " ";
- }
- }
- int main()
- {
- const int N = 50;
- int a[N];
- srand(time(NULL));
- for(int i(0);i<N;i++)
- a[i] = rand()%100+1;
- cout << "Array before\n";
- PrintArr(a,N);
- double t1 = clock();
- QuickSort(a,0,N-1);
- double t2 = clock() - t1;
- cout << "\nArray after\n";
- PrintArr(a,N);
- cout << "\n Time to rearrange = " << t2/CLOCKS_PER_SEC << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement