-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path51_quickSort.cpp
77 lines (66 loc) · 1.72 KB
/
51_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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
using namespace std;
void fillArray(int arr[],int n){
for(int i=0; i<n; i++){
cout<<i<<": ";
cin>>arr[i];
}
}
void displayArray(int arr[],int n){
for(int i=0; i<n; i++){
cout<<arr[i]<<" ";
}
}
int partition(int arr[],int start,int end){
int pivot = arr[start];
int cnt = 0;
// finding the count of smaller elements than pivot
for(int i=start+1; i<=end; i++){
if(arr[i]<=pivot)
cnt++;
}
// placing element at right position
int pivotIndex = start + cnt;
swap(arr[pivotIndex],arr[start]);
// now moving all smaller elements on left and greater on right
int i = start, j = end;
while(i<pivotIndex && j>pivotIndex){
while(arr[i] <= pivot){
i++;
}
while(arr[j] > pivot){
j--;
}
if(i<pivotIndex && j>pivotIndex){
swap(arr[i++],arr[j--]);
}
}
return pivotIndex;
}
void quickSort(int arr[],int start, int end){
//base case
if(start >= end){
return;
}
// get the position of the element
int p = partition(arr,start,end);
// recursive call for left partition
quickSort(arr,start,p-1);
// recursive call for right partition
quickSort(arr,p+1,end);
}
int main(){
int n;
cout<<"Enter the size of the array: ";
cin>>n;
int *arr = new int[n];
cout<<endl<<"Fill the array..."<<endl<<endl;
fillArray(arr,n);
cout<<endl<<endl;
cout<<"Sorting the array..."<<endl;
quickSort(arr,0,n-1);
cout<<endl;
cout<<"Displaying the sorted array..."<<endl;
displayArray(arr,n);
return 0;
}