-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathquick_sort.cpp
More file actions
39 lines (36 loc) · 846 Bytes
/
quick_sort.cpp
File metadata and controls
39 lines (36 loc) · 846 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
#include <iostream>
using namespace std;
int partition(int arr[], int s, int e){
int i=s-1;
int j = s;
int pivot = arr[e];
for(; j<=e -1; ){
if(arr[j]<= pivot){
//merge the smaller element in region 1
i = i+1;
swap(arr[i], arr[j]);
}
//expand the larger region
j++;
}
//place the pivot element in the correct position
swap(arr[i+1], arr[e]);
return i+1;
}
void quickSort(int arr[], int s, int e){
if(s>=e){
return;
}
int p = partition(arr,s,e);
quickSort(arr,s,p-1);//left part of arr
quickSort(arr, p, e); //right part of arr
}
int main() {
int arr[] = {2,1,4,5,6,3,7};
int n = sizeof(arr)/sizeof(int);
quickSort(arr,0,n-1);
for(int i=0; i<n; i++){
cout<<arr[i]<<" ";
}
return 0;
}