-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqsort.c
54 lines (42 loc) · 870 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
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
struct Stu
{
char name[20];
int age;
};
int cmp_stu_by_age(const void* e1, const void* e2)
{
return ((struct Stu*)e1)->age - ((struct Stu*)e2)->age;
}
int cmp_stu_by_name(const void* e1, const void* e2)
{
return strcmp(((struct Stu*)e1)->name,((struct Stu*)e2)->name);
}
int cmp_float(const void* e1, const void* e2)
{
return (int)(*(float*)e1 - *(float*)e2);
}
void test3()
{
struct Stu s[3] = { {"zhangsan",20},{"lisi",10},{"wangwu",30} };
int sz = sizeof(s) / sizeof(s[0]);
qsort(s, sz, sizeof(s[0]), cmp_stu_by_name);
}
void test2()
{
float f[] = { 11.0,21.0,33.0,44.0,25.0,16.0,17.0,28.0 };
int sz = sizeof(f) / sizeof(f[0]);
qsort(f, sz, sizeof f[0], cmp_float);
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%f ", f[i]);
}
}
int main()
{
test2();
return 0;
}