-
Notifications
You must be signed in to change notification settings - Fork 0
/
qsort.h
54 lines (41 loc) · 791 Bytes
/
qsort.h
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
46
47
48
49
50
51
52
53
54
#ifndef QSORT_H
#define QSORT_H
#include <stdlib.h>
#include <time.h>
void swap(int l, int r) {
int tmp;
tmp = l;
l = r;
r = tmp;
}
int part(int arr[], int low, int high) {
int pivot;
int i;
int j;
pivot = arr[low];
i = low;
j = low + 1;
while (j <= high) {
if (arr[j] < pivot) {
swap(arr[i = i + 1], arr[j]);
}
}
swap(arr[low], arr[i]);
return i;
}
int rpart(int arr[], int low, int high) {
int rnum;
srand((unsigned)time(0));
rnum = low + rand() % (high - low);
swap(arr[rnum], arr[low]);
return part(arr, low, high);
}
void rqsort(int arr[], int low, int high) {
int pivot;
if (low < high) {
pivot = rpart(arr, low, high);
rqsort(arr, low, pivot-1);
rqsort(arr, pivot + 1, high);
}
}
#endif /* QSORT_H */