-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuick Sort1.cpp
87 lines (79 loc) · 1.62 KB
/
Quick Sort1.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
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <bits/stdc++.h>
using namespace std;
template <class T> inline T bigmod(T b, T p, T m)
{
T ret;
if(p==0) return 1;
if(p&1)
{
ret=(bigmod(b,p/2,m)%m);
return ((b%m)*ret*ret)%m;
}
else
{
ret=(bigmod(b,p/2,m)%m);
return (ret*ret)%m;
}
}
/* template functions */
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int>pii;
typedef pair<LL, LL>pll;
typedef vector<int>vi;
typedef vector<LL>vll;
typedef vector<pii>vpii;
typedef vector<pll>vpll;
/* type definition */
int dx4[]= {1,-1,0,0};
int dy4[]= {0,0,1,-1};
int dx6[]= {0,0,1,-1,0,0};
int dy6[]= {1,-1,0,0,0,0};
int dz6[]= {0,0,0,0,1,-1};
int dx8[]= {1,-1,0,0,-1,1,-1,1};
int dy8[]= {0,0,1,-1,1,1,-1,-1};
int dkx8[]= {-1,1,-1,1,-2,-2,2,2};
int dky8[]= {2,2,-2,-2,1,-1,1,-1};
/* direction array */
int tc=1;
const double eps=1e-9;
const double pi=acos(-1.0);
const long long int mx=1e5;
const long long int mod=1e9+7;
/* global declaration */
int a[mx+5],n;
int partition(int l, int r)
{
int i,j;
i=l-1;
for(j=l; j<r; j++)
{
if(a[j]<=a[r]) swap(a[++i],a[j]);
}
swap(a[++i],a[r]);
return i;
}
void quick_sort(int l, int r)
{
if(l<r)
{
int m=partition(l,r);
quick_sort(l,m-1);
quick_sort(m+1,r);
}
return;
}
int main()
{
int i;
while(cin>>n)
{
for(i=1; i<=n; i++)
scanf("%d",&a[i]);
quick_sort(1,n);
for(i=1; i<=n; i++)
printf("%d ",a[i]);
printf("\n");
}
return 0;
}