Professional Documents
Culture Documents
P 3
P 3
#include <iostream>
#include <cstdlib>
#include<time.h>
using namespace std;
int partition(int *a, int s, int e)
{ int pivot = a[s]; int i = s
+ 1; for (int j = s + 1; j <= e;
j++) { if (a[j] < pivot) {
swap(a[i], a[j]); i++;
} }
swap(a[s], a[i - 1]);
return i - 1;
}
int pick_random(int *a, int s, int e)
{ srand(time(0)); int r = s + rand() % (e - s);
cout << "Randomly selected pivot index: " << r << endl;
swap(a[r], a[s]);
return partition(a, s, e);
}
void quick_sort(int *a, int s, int e)
{ if (s >= e) return;
int p = pick_random(a, s,
e);
cout << "Array after
partition: ";
OUTPUT :