qsort 函数的使用
来源:互联网 发布:淘宝韩版女装店铺推荐 编辑:程序博客网 时间:2024/05/16 05:54
该函数是我在看x264代码中关于参考帧列表的排序接触到的,总结于此,以作备忘。
功 能: 使用快速排序例程进行排序
原 型: void qsort(void *base,int nelem,int width,int (*fcmp)(const void *,const void *));
参 数: 1. 待排序数组首地址
2. 数组中待排序元素数量
3. 各元素的占用空间大小
4. 指向函数的指针,用于确定排序的顺序
下面给出对一维int型数组进行排序的实例,其它数据类型的数组排序原理相同,不重复给出例子了。
#include <stdio.h>#include <stdlib.h> //!< 这个是qsort所在的头文件int cmp_desc(const void *a, const void *b)//!< 降序{if (*((int *)a) < *((int *)b))return 1;if (*((int *)a) > *((int *)b))return -1;elsereturn 0;}int cmp_asc(const void *a, const void *b)//!< 升序{if (*((int *)a) > *((int *)b))return 1;if (*((int *)a) < *((int *)b))return -1;elsereturn 0;}int main(){int aa[10] = {2, 43, 23, 34, 6, 7, 3, 10, 24, 54};qsort(aa, 10, sizeof(int), cmp_desc);printf("降序排序!\n");for (int i = 0; i< 10; i++)printf("%d\t", aa[i]);printf("升序排序!\n");qsort(aa, 10, sizeof(int), cmp_asc);for (int i = 0; i< 10; i++)printf("%d\t", aa[i]);return 0;}
下图给出运行结果:
- qsort 函数的使用
- qsort函数的使用
- 备忘qsort函数的使用
- qsort函数的使用问题
- qsort函数和bsearch函数的使用
- qsort函数和bsearch函数的使用
- qsort函数和bsearch函数的使用
- 杂记之函数qsort的使用
- stdlib库中qsort函数的使用
- C语言qsort函数的使用
- qsort函数使用大全
- qsort函数使用
- qsort()函数使用
- qsort()函数使用
- 在c++中qsort()排序函数的使用qsort函数应用大全
- 在c 中qsort()排序函数的使用qsort函数应用大全
- 在c++中qsort()排序函数的使用qsort函数应用大全
- c++中qsort()排序函数的使用qsort函数应用大全
- JavaScript使用小技巧:IE8的关闭处理
- Objective-C基础教程读书笔记
- LINUX_FS_H
- 高效工作——EA使用技巧
- 《Windows via C/C++》学习笔记 —— Windows 线程池
- qsort 函数的使用
- php输出、写入csv
- 在IE6里无法识别first-letter/:first-line?
- Application cannot be run in virtual machine.
- Codeforces Round #154 (Div. 2)
- 杭电OJ题 1391 Number Steps 解题报告
- 初始化条件变量pthread_cond_init
- Hibernate 之 Persistence
- GNU C