forked from dreamerHarshit/Coding_Questions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuick.cpp
More file actions
44 lines (35 loc) · 696 Bytes
/
Quick.cpp
File metadata and controls
44 lines (35 loc) · 696 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
#include <iostream>
#define parr(arr,s) for(int i=0;i<s;cout<<arr[i++]<<" ");cout<<endl;
#define sarr(arr,s) for(int i=0;i<s;cin>>arr[i++]);
using namespace std;
void swap(int *a, int *b){
int tmp = *a;
*a = *b;
*b = tmp;
}
int partition(int a[],int l,int h){
int i=l,j=h,pivot=a[l];
do{
do{i++;}while(a[i]<=pivot);
do{j--;}while(a[j]>pivot);
if(i<j){
swap(&a[i],&a[j]);
}
}while(i<j);
swap(&a[j],&a[l]);
return j;
}
void quiksrt(int a[],int l,int h){
if(l<h){
int j = partition(a,l,h);
quiksrt(a,l,j);
quiksrt(a,j+1,h);
}
}
int main(){
int arr[6];
arr[6] = INT_MAX;
sarr(arr,5);
quiksrt(arr,0,5);
parr(arr,5);
}