-
Notifications
You must be signed in to change notification settings - Fork 0
/
quick.c
45 lines (39 loc) · 811 Bytes
/
quick.c
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
//
// Created by ge on 2024/1/2.
//
#include <stdio.h>
int partition(int arr[], int L, int R) {
int pivot = R;
int j = L;
for (int i = L; i < R; ++i) {
if (arr[i] < arr[pivot]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j++;
}
}
int temp = arr[j];
arr[j] = arr[R];
arr[R] = temp;
return j;
}
void quickSort(int arr[], int L, int R) {
if (L < R) {
int M = partition(arr,L,R);
quickSort(arr,L,M-1);
quickSort(arr,M+1, R);
}
}
void printArr(int arr[], int len) {
for (int i = 0; i < len; i++) {
printf("%d\t", arr[i]);
}
printf("\n");
}
int main() {
int arr[] = {3, 8, 1, 2, 5, 4};
quickSort(arr, 0, 5);
printArr(arr, 6);
return 0;
}