-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathQuickSort.cpp
More file actions
42 lines (42 loc) · 816 Bytes
/
Copy pathQuickSort.cpp
File metadata and controls
42 lines (42 loc) · 816 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
#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[high];
int i=low-1;
for(int j=low;j<=high-1;j++)
{
if(a[j]<pivot)
{
i++;
swap(a[i],a[j]);
}
}
swap(a[i+1],a[high]);
return i+1;
}
void quicksort(int *a,int low,int high)
{
if(low<high)
{
int pi=qpartition(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;
}