-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.cpp
More file actions
49 lines (49 loc) · 1.07 KB
/
QuickSort.cpp
File metadata and controls
49 lines (49 loc) · 1.07 KB
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
#include<iostream>
using namespace std;
void printArray(int *A, int n){
for(int i = 0; i<n; i++){
cout<<A[i]<<" ";
}
cout<<endl;
}
int partition(int *A, int l, int h){
int pivot = A[l];
int i = l +1;
int j = h;
do{
while(A[i]<=pivot){
i++;
}
while(A[j]>pivot){
j--;
}
if(i<j){
//swapping A[i] and A[j]
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}while(i<j);
//swap A[low] and A[j]
int temp = A[l];
A[l] = A[j];
A[j] = temp;
return j;
}
void quickSort(int A[], int l, int h){
int partitionIndex;
if(l<h){
partitionIndex = partition(A, l, h);
quickSort(A, l, partitionIndex-1);
quickSort(A, partitionIndex+1, h);
}
}
int main(){
int A[] = {12, 54, 65, 7, 23, 9};int n = sizeof(A)/sizeof(int);
cout<<"Printing array before Sorting"<<endl;
printArray(A,n );
quickSort(A, 0, 5);
cout<<"Printing array after Sorting"<<endl;
printArray(A, n);
return 0;
}