forked from rishabhgarg25699/Competitive-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Randomized_quick_sort.cpp
74 lines (73 loc) · 956 Bytes
/
Randomized_quick_sort.cpp
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
struct pivot
{
ll m1;
ll m2;
};
void swap(ll arr[],ll i,ll j)
{
ll temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
void part_r(ll arr[],ll l,ll r)
{
ll x=l+rand()%(r-l);
swap(arr,l,x);
}
pivot partition(ll arr[],ll l,ll r)
{
part_r(arr,l,r);
ll i,j=l,k=l,c=0;
pivot p;
for(i=l+1;i<=r;i++)
{
if(arr[i]==arr[l])
{
k++;
j++;
swap(arr,j,i);
swap(arr,k,j);
}
else if(arr[i]<arr[l])
{
j++;
swap(arr,i,j);
}
}
for(i=l;i<=k;i++)
{
swap(arr,i,j-c);
c++;
}
k=j-c+1;
p.m1=k;
p.m2=j;
return p;
}
void qsort(ll arr[],ll l,ll r)
{
if(l<r)
{
pivot m=partition(arr,l,r);
qsort(arr,l,m.m1-1);
qsort(arr,m.m2+1,r);
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cut.tie(NULL);
ll n,i;
cin>>n;
ll arr[n+100];
for(i=0;i<n;i++)
cin>>arr[i];
qsort(arr,0,n-1);
for(i=0;i<n;i++)
cout<<arr[i]<<" ";
return 0;
}