前言
今天分享一个库函数
介绍qsort的使用及实现方法
他可以实现不限于整形、浮点型、字符型、自定义等类型的排序
qsort的简单介绍
qsort | |
头文件 | #include <stdlib.h> |
格式 | void qsort(void* base,size_t num,size_t width,int(__cdecl*compare(const void*,const void*)) |
功能 | 实现多类型的快速排序 |
返回值 | 无返回值 |
把格式分解
1
2
3
4
5
|
void qsort ( void * base, size_t num, size_t width, int (* compare)( const void * e1, const void * e2) ); |
qsort的首参数为待排列数组的首地址
size_t num为某个类型的个数
size_t width为类型的宽度,也就是该类型的大小
int(* compare)(const void* e1, const void* e2)为比较函数的指针,这里是利用函数指针作为参数,实现传参
这里运用了回调函数的思想
回调函数就是通过函数指针调用的函数,如果把一个函数的指针(地址)当作参数,传给另一个函数,当这个函数调用所指的函数时,我们就说这是回调函数。
用qsort实现一个整形类型的排序
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
|
#include<stdio.h> #include<stdlib.h> int cmp_int( const void * e1, const void * e2) { return (*( int *)e1) - (*( int *)e2); } void test1() { int arr[] = { 1,4,2,6,5,3,7,9,0,8 }; int sz = sizeof (arr) / sizeof (arr[0]); qsort (arr, sz, sizeof (arr[0]), cmp_int); int i = 0; for (i = 0; i < sz; i++) { printf ( "%d " , arr[i]); } printf ( "\n" ); } int main() { test1(); return 0; } |
用qsort函数实现结构体的排序
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
62
|
#include<stdio.h> #include<stdlib.h> struct Stu { char name[20]; int age; float score; }; int cmp_stu_by_socre( const void * e1, const void * e2) //结构体中的浮点型 { if ((( struct Stu*)e1)->score > (( struct Stu*)e2)->score) { return 1; } else if ((( struct Stu*)e1)->score < (( struct Stu*)e2)->score) { return -1; } else { return 0; } } 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); } void print_stu( struct Stu arr[], int sz) //打印函数 { int i = 0; for (i = 0; i < sz; i++) { printf ( "%s %d %f\n" , arr[i].name, arr[i].age, arr[i].score); } printf ( "\n" ); } void test4() { struct Stu arr[] = { { "zhangsan" ,20,87.5f},{ "lisi" ,22,99.0f},{ "wangwu" , 10, 68.5f} }; //按照成绩来排序 int sz = sizeof (arr) / sizeof (arr[0]); qsort (arr, sz, sizeof (arr[0]), cmp_stu_by_socre); print_stu(arr, sz); qsort (arr, sz, sizeof (arr[0]), cmp_stu_by_age); print_stu(arr, sz); qsort (arr, sz, sizeof (arr[0]), cmp_stu_by_name); print_stu(arr, sz); } int main() { test4(); return 0; } |
结构体的成员包括整形,浮点型和字符型
分别从小到大来排列
cmp函数通过返回值的正负性来实现比较大小,就可以实现下图的结果
qsort函数的实现
qsort函数在实现的时候,其实跟冒泡排序有一定的联系
只不过
相对于冒泡排序,它可以排序各类型的数据,下面通过对比来实现其函数的功能
冒泡排序实现整形的排序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
void bubble( int arr[], int sz) //冒泡排序实现整形的排序 { int tmp = 0; int i = 0; int j = 0; for (i = 0; i < sz - 1; i++) { for (j=0;j<sz-1;j++) { if (arr[j] > arr[j + 1]) { tmp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = tmp; } } } } |
qsort函数的实现
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
|
void Swap( char * buf1, char * buf2, int width) { int i = 0; for (i = 0; i < width; i++) { char tmp = *buf1; *buf1 = *buf2; *buf2 = tmp; buf1++; buf2++; } } void bubble_sort( void * base, int sz, int width, int (*cmp)( const void * e1, const void * e2)) { int i = 0; for (i = 0; i < sz - 1; i++) { int j = 0; for (j = 0; j < sz - 1 - i; j++) { //if (arr[j] > arr[j + 1]) if (cmp(( char *)base + j * width, ( char *)base + (j + 1) * width) > 0) { //两个元素的交换 Swap(( char *)base + j * width, ( char *)base + (j + 1) * width, width); } } } } |
可以看出有两个地方有差异
首先就是两个元素比较大小,是通过cmp比较函数实现
当返回值大于零,函数就实现从小到大来排序
当返回值小于零,函数就实现从大到小来排序
当返回值等于零,元素不发生变化
第二个差异就是,实现两个元素的交换
冒泡排序就是通过引入一个中间变量,达到交换的目的
而qsort函数,通过调用一个函数,通过引入宽度(所占字节的大小),进行字节之间的交换,所以用char类型来实现不同类型的交换,所以首先需要知道排序数组内每一个元素的大小,整形就交换四个字节的空间即可。
到此这篇关于C语言 使用qsort函数来进行快速排序的文章就介绍到这了,更多相关C语言 qsort函数内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://z-ming.blog.csdn.net/article/details/122774748