-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.cpp
More file actions
47 lines (47 loc) · 922 Bytes
/
quick_sort.cpp
File metadata and controls
47 lines (47 loc) · 922 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
#include<bits/stdc++.h>
using namespace std;
int partition(vector<int>&arr, int low, int high)
{
int pivot=arr[low];
int i=low+1, j=high;
while(i<=j)
{
while(i<=high && arr[i]<=pivot)
{
i++;
}
while(j>=low && arr[j]>pivot)
{
j--;
}
if(i<j)
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
swap(arr[low], arr[j]);
return j;
}
void quick_sort(vector<int>&arr, int low, int high)
{
if(low<high)
{
int p=partition(arr, low, high);
quick_sort(arr, low, p-1);
quick_sort(arr, p+1, high);
}
}
int main()
{
int n;
cin>>n;
vector<int>arr(n);
for(int i=0; i<n; i++)
cin>>arr[i];
quick_sort(arr, 0, n-1);
for(int i:arr)
cout<<i<<" ";
return 0;
}