-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRandomizedQuickSort.cpp
More file actions
49 lines (49 loc) · 974 Bytes
/
Copy pathRandomizedQuickSort.cpp
File metadata and controls
49 lines (49 loc) · 974 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <bits/stdc++.h>
#define N 1000000007
#define M 1000000
using namespace std;
typedef long long int lli;
int qpartition(int *a,int low,int high)
{
int pivot=a[low];
int i=high+1;
for(int j=high;j>=low+1;j--)
{
if(a[j]>pivot)
{
i--;
swap(a[i],a[j]);
}
}
swap(a[i-1],a[low]);
return i-1;
}
int partition_r(int *a,int low,int high)
{
int el=high-low+1;
int r=rand()%el;
swap(a[low],a[low+r]);
return qpartition(a,low,high);
}
void quicksort(int *a,int low,int high)
{
if(low<high)
{
int pi=partition_r(a,low,high);
quicksort(a,low,pi-1);
quicksort(a,pi+1,high);
}
}
int main()
{
int n;
cin>>n;
int *a=(int *)malloc(n*sizeof(int));
for(int i=0;i<n;i++)
cin>>a[i];
quicksort(a,0,n-1);
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
return 0;
}