-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.cpp
More file actions
46 lines (38 loc) · 807 Bytes
/
QuickSort.cpp
File metadata and controls
46 lines (38 loc) · 807 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
#include <iostream>
#include <vector>
using namespace std;
void swap(int &a, int &b){
int t = a;
a = b;
b = t;
}
int partition(int arr[], int low, int high){
int pi = arr[low];
int i = low;
int j = high;
while(i < j){
do{++i;}while(arr[i] <= pi);
do{--j;} while(arr[j] > pi);
if(i<j)
swap(arr[i], arr[j]);
}
swap(arr[j], arr[low]);
return j;
}
void QuickSort(int arr[], int low, int high){
int p;
if(low< high){
p = partition(arr, low, high);
QuickSort(arr, low, p);
QuickSort(arr, p+1, high);
}
}
int main()
{
int arr[] = {9, 2, 3, 6, 1, 0, 5, 8};
QuickSort(arr, 0, 8);
for(int i = 0; i < 8; ++i){
std::cout << arr[i] << endl;
}
return 0;
}