-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patht.c
More file actions
38 lines (32 loc) · 896 Bytes
/
t.c
File metadata and controls
38 lines (32 loc) · 896 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
#include <stdio.h>
void quicksort(int *, int);
void partition(int *, int, int);
void swap(int *, int, int);
int main() {
int nums[] = {4,5,6,6,7,3,2,2,1};
for (int i = 0; i < 9; ++i) printf("%d ", nums[i]);
printf("\n");
quicksort(nums, 9);
for (int i = 0; i < 9; ++i) printf("%d ", nums[i]);
}
void quicksort(int * nums, int len) {
partition(nums, 0, len - 1);
}
void partition(int * nums, int left, int right) {
if (left >= right) return;
int pivot = nums[left];
int lo = left + 1, hi = right;
while (lo <= hi) {
if (nums[lo] > pivot) swap(nums, lo, hi--);
else ++lo;
}
swap(nums, left, hi);
partition(nums, left, hi - 1);
partition(nums, hi + 1, right);
}
void swap(int * nums, int left, int right) {
if (left == right) return;
int temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
}