forked from shinh/elvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qsort.c
61 lines (54 loc) · 988 Bytes
/
qsort.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdio.h>
#include <stdlib.h>
int cmp_int(const void* a, const void* b) {
return *(int*)a - *(int*)b;
}
typedef struct {
int x, y;
} S;
int cmp_s(const void* a, const void* b) {
S* sa = (S*)a;
S* sb = (S*)b;
return sa->x * sa->y - sb->x * sb->y;
}
void test1() {
puts("test1");
int data[] = {
3, 5, 7, 1, 4, 2
};
qsort(data, 6, sizeof(int), cmp_int);
for (int i = 0; i < 6; i++) {
printf("%d\n", data[i]);
}
}
void test2() {
puts("test2");
int data[] = {
3, 5, 7, 5, 1, 4, 2
};
qsort(data, 7, sizeof(int), cmp_int);
for (int i = 0; i < 7; i++) {
printf("%d\n", data[i]);
}
}
void test3() {
puts("test3");
S data[] = {
{ 3, 3 },
{ 5, 2 },
{ 7, 1 },
{ 5, 4 },
{ 1, 11 },
{ 4, 3 },
{ 2, 1 }
};
qsort(data, 7, sizeof(S), cmp_s);
for (int i = 0; i < 7; i++) {
printf("%d = %d * %d\n", data[i].x * data[i].y, data[i].x, data[i].y);
}
}
int main() {
test1();
test2();
test3();
}