-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathquick-sort.cpp
More file actions
45 lines (32 loc) · 735 Bytes
/
quick-sort.cpp
File metadata and controls
45 lines (32 loc) · 735 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
# include <iostream>
using namespace std;
int partition (int array[], int start, int end) {
int pivot = array[end];
int j = start;
for (int i = 1; i < end; ++i) {
if (array[i] <= pivot) {
++j;
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
int temp = array[j+1];
array[j+1] = array[end];
array[end] = temp;
return (j+1);
}
void sort (int array[], int start, int end) {
if (start < end) {
int index = partition (array, start, end);
sort (array, start, index-1);
sort (array, index+1, end);
}
}
int main () {
int n, array[] = { 5, 4, 3, 2, 1};
n = sizeof(array)/sizeof(array[0]);
sort (array, 0 , n-1);
for (int i = 0; i < n; ++i)
cout << array[i] << " ";
}