-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort
More file actions
44 lines (40 loc) · 955 Bytes
/
quick_sort
File metadata and controls
44 lines (40 loc) · 955 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
#include <iostream>
#include <stdio.h>
int quick_sort(int *a, int left, int right);
int main(){
using namespace std;
int a[10] = {5, 3, 4, 7, 6, 9, 2, 8, 1};
int N_1 = 10;
quick_sort(a, 0, N_1-1);
for (int i = 0; i < N_1; i++) {
cout << a[i] << " ";
}
}
int quick_sort(int *a, int left, int right){
if (left == right || left == right+1)
return 0;
else {
int i = left;
int j = right;
int temp = a[i];
while (i < j) {
while (i < j && a[j] <= temp) {
j = j - 1;
}
if (i < j) {
a[i] = a[j];
i++;
}
while (i < j && a[i] >= temp) {
i = i + 1;
}
if (i < j) {
a[j] = a[i];
j--;
}
}
a[i] = temp;
quick_sort(a, left, i-1);
quick_sort(a, i+1, right);
}
}