forked from InvisiblePro/Hacktoberfest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quicksort.cpp
52 lines (47 loc) · 917 Bytes
/
Quicksort.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
#include <iostream>
using namespace std;
int partition(int*arr,int s,int e){
int pivot = arr[s];
int count= 0;
for(int i = s+1;i<=e;i++){
if(arr[i]<=pivot){
count ++;
}
}
int pInd= count+s;
swap(arr[pInd],arr[s]);
int i=s, j= e;
while(i<pInd &&j>pInd){
while(arr[i]<pivot){
i++;
}
while(arr[j]>pivot){
j--;
}
if(i<pInd &&j>pInd){
swap(arr[i++],arr[j--]);
}
}return pInd;
}
void quickSort(int *arr, int s, int e){
if(s>=e){
return;
}
int pivot = partition(arr,s,e);
quickSort(arr,s,pivot-1);
quickSort(arr,pivot+1,e);
}
int main()
{
int n;
cin>>n;
int arr[n];
for(int i = 0;i<n; i ++){
cin>>arr[i];
}
quickSort(arr,0,n-1);
for(int i = 0;i<n; i ++){
cout<<arr[i];
}
return 0;
}