-
Notifications
You must be signed in to change notification settings - Fork 6
/
quicksort.cpp
58 lines (48 loc) · 1.04 KB
/
quicksort.cpp
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
50
51
52
53
54
55
56
57
58
# http://codereview.stackexchange.com/questions/77782/quick-sort-implementation
#include <iostream>
using namespace std;
void print(int *a, int n)
{
int i=0;
while(i<n){
cout<<a[i]<<",";
i++;
}
}
void swap(int i,int j, int *a){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
void quicksort(int *arr, int left, int right){
int min = (left+right)/2;
cout<<"QS:"<<left<<","<<right<<"\n";
int i = left;
int j = right;
int pivot = arr[min];
while(left<j || i<right)
{
while(arr[i]<pivot)
i++;
while(arr[j]>pivot)
j--;
if(i<=j){
swap(i,j,arr);
i++;
j--;
}
else{
if(left<j)
quicksort(arr, left, j);
if(i<right)
quicksort(arr,i,right);
return;
}
}
}
int main() {
int arr[8] = {110, 5, 10,3 ,22, 100, 1, 23};
quicksort(arr, 0, (sizeof(arr)/sizeof(arr[0]))-1);
print(arr, (sizeof(arr)/sizeof(arr[0])));
return 0;
}