forked from anuragsawle/Data-Structure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort1.cpp
More file actions
46 lines (41 loc) · 754 Bytes
/
quickSort1.cpp
File metadata and controls
46 lines (41 loc) · 754 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
#include <bits/stdc++.h>
#define ll long long
#define FAST1 ios_base::sync_with_stdio(false);
#define FAST2 cin.tie(NULL);
using namespace std;
void swap(int a[],int i,int j){
int temp = a[i];
a[i]=a[j];
a[j]=temp;
}
int partition(int a[],int l,int r){
int pivot = a[r];
int i=l-1;
int j=l;
while(j<r){
if(a[j]<pivot){
i++;
swap(a,i,j);
}
j++;
}
swap(a,i+1,r);
return i+1;
}
void quickSort(int a[],int l,int r){
if(l<r){
int p = partition(a,l,r);
quickSort(a,l,p-1);
quickSort(a,p+1,r);
}
}
int main() {
FAST1;
FAST2;
int v[] = {17,2,41,54,1};
quickSort(v,0,4);
for(int i=0;i<=4;i++){
cout<<v[i]<<" ";
}
return 0;
}