-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathquick sort.cpp
More file actions
51 lines (38 loc) · 774 Bytes
/
quick sort.cpp
File metadata and controls
51 lines (38 loc) · 774 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
#include<bits/stdc++.h>
using namespace std;
int partition(int arr[], int s, int e){
// inplace partition
int pivot = e;
int i=s-1;
for(int j=s; j<e; j++){
if(arr[j]<=arr[pivot]){
i++;
swap(arr[i], arr[j]);
}
if(arr[j]>arr[pivot]){
// j already increases
}
}
// Placing pivot element at correct place
swap(arr[i+1], arr[pivot]);
return i+1;
}
void quicksort(int arr[], int s, int e){
// Base Case
if(s>=e){
// One or no element is present in the array.
// So it is sorted.
return;
}
int p=partition(arr, s, e);
quicksort(arr, s, p-1);
quicksort(arr, p+1, e);
}
int main(){
int arr[]={2,7,8,6,1,5,4};
int n = sizeof(arr)/sizeof(arr[0]);
quicksort(arr, 0, n-1);
for(int i=0; i<n; i++){
cout<<arr[i]<<" ";
}
}