-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.cpp
More file actions
42 lines (40 loc) · 905 Bytes
/
Copy pathQuickSort.cpp
File metadata and controls
42 lines (40 loc) · 905 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
#include <iostream>
using namespace std;
int setPivot(int* arr, int l, int r){
int pivot = (l+r)/2;
int temp;
int i = l;
int j= r;
while (true){
while (arr[i] < arr[pivot]){
i++;
}
while (arr[j] > arr[pivot]){
j--;
}
if (i>=j)
{
return j;
}
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
void QuickSort(int* arr, int l, int r){
int index = setPivot(arr,l,r);
cout<<"new Pivot after setPivot Function: "<<index<<endl;
if (l<r){
cout<<"Right: "<<r<<" Left: "<<l<<endl;
QuickSort(arr,l,index);
QuickSort(arr,index+1,r);
}
}
int main(){
int arr[10] = {78,92,55,62,77,97,11,52,88,29};
QuickSort(arr,0,9);
for (size_t i = 0; i < sizeof(arr)/sizeof(int); i++){
cout<<arr[i]<<" ";
}
return 0;
}