-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquicksort.cpp
More file actions
40 lines (37 loc) · 920 Bytes
/
quicksort.cpp
File metadata and controls
40 lines (37 loc) · 920 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
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
void quickSort(vector<int>& arr, int low, int high) {
// code here
if(low>=high) return;
int pivot_index=partition(arr,low,high);
quickSort(arr,low,pivot_index-1);
quickSort(arr,pivot_index+1,high);
}
public:
int partition(vector<int>& arr, int low, int high) {
// code here
int pivot=arr[high];
int i=low-1;
int j=low;
for(;j<high;j++){
if(arr[j]<pivot){
i++;
swap(arr[i],arr[j]);
}
}
swap(arr[i+1],arr[high]);
return i+1;
}
};
int main() {
vector<int> arr = {4, 5, 10, 9, 7, 89, 1, 3};
Solution sol;
sol.quickSort(arr,0,7);
for (int i = 0; i < arr.size(); i++) {
cout << arr[i] << " ";
}
cout << endl;
}