-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort.cpp
More file actions
53 lines (47 loc) · 869 Bytes
/
quickSort.cpp
File metadata and controls
53 lines (47 loc) · 869 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
48
49
50
51
52
53
#include<iostream>
using namespace std;
void swap(int *a, int *b){
int temp = *a;
*a = *b;
*b = temp;
}
int partition(int *a,int l,int h){
int pivot = a[l];
int i = l, j = h;
while(i<j){
do{
i++;
}
while(a[i] <= pivot);
do{
j--;
}
while(a[j] > pivot);
if(i<j){
swap(&a[i],&a[j]);
}
}
swap(&a[l],&a[j]);
return j;
}
void quickSort(int *a, int l, int h){
if(l<h){
int j = partition(a,l,h);
quickSort(a,l,j);
quickSort(a,j+1,h);
}
}
void printArray(int *a,int n){
cout<<"Array is ->";
for(int i=0; i<n; i++){
cout<<a[i]<<" ";
}
cout<<endl;
}
int main(){
int a[] = {1,3,2,5,7,6,4,8};
printArray(a,8);
quickSort(a,0,8);
printArray(a,8);
return 0;
}