-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathl36QuickSort.cpp
More file actions
36 lines (36 loc) · 840 Bytes
/
l36QuickSort.cpp
File metadata and controls
36 lines (36 loc) · 840 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
#include<iostream>
using namespace std;
int partition(int arr [], int s, int e){
int pivot = arr[s];
int cnt = 0;
for(int i = s+1; i<=e; i++){
if(arr[i]<=pivot){
cnt ++;
}
}
// place pivot at right place
int pivotIndex = s+ cnt;
swap(arr[pivotIndex], arr[s]);
int i = s, j = e;
while( i<pivotIndex && j>pivotIndex+1){
while(arr[i]<pivot){
i++;
}
while(arr[i]>pivot){
j--;
}
if(i<pivotIndex && j>pivotIndex){
swap(arr[i++], arr[j--]);
}
}
}
void quickSort(int arr[], int s, int e){
// base case
if(s>=e){//zero element--> sorted,, one element -->sorted
return;
}
int p = partition(arr, s, e);
quickSort(arr, s, p-1);
quickSort(arr, p+1, e);
}
int main(){}