-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.c
More file actions
41 lines (35 loc) · 770 Bytes
/
quick_sort.c
File metadata and controls
41 lines (35 loc) · 770 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
#include <stdio.h>
int partition(int A[], int low, int high){
int i, j, temp;
int pivot = A[high];
for(i = low - 1, j = low; j < high; j++){
if(A[j] < pivot){
i++;
temp = A[j];
A[j] = A[i];
A[i] = temp;
}
}
temp = A[high];
A[high] = A[i + 1];
A[i+1] = temp;
return i + 1;
}
void quick_sort(int A[], int low, int high){
if(low >= high){
return;
}
int p = partition(A, low, high);
quick_sort(A, low, p - 1);
quick_sort(A, p + 1, high);
return;
}
int main(){
int n = 10;
int A[] = {9, 5, 36, 41, 57, 61, 57, 5, 31, 28};
quick_sort(A, 0, n - 1);
for(int i = 0; i < n; i++){
printf("%d ", A[i]);
}
return 0;
}