



全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C语言标准库函数 qsort 详解qsort包含在头文件中,此函数根据你给的比较条件进行快速排序,通过指针移动实现排序。排序之后的结果仍然放在原数组中。使用qsort函数必须自己写一个比较函数。函数原型:void qsort ( void * base, size_t num, size_t size, int ( * comparator ) ( const void *, const void * ) );用法以及参数说明:Sorts the num elements of the array pointed by base, each element size bytes long, using the comparator function to determine the order.The sorting algorithm used by this function compares pairs of values by calling the specified comparator function with two pointers to elements of the array.The function does not return any value, but modifies the content of the array pointed by base reordering its elements to the newly sorted order.base Pointer to the first element of the array to be sorted.(数组起始地址)num Number of elements in the array pointed by base.(数组元素个数)size Size in bytes of each element in the array.(每一个元素的大小)comparator Function that compares two elements.(函数指针,指向比较函数)1、The function must accept two parameters that are pointers to elements, type-casted as void*. These parameters should be cast back to some data type and be compared.2、The return value of this function should represent whether elem1 is considered less than, equal to, or greater than elem2 by returning, respectively, a negative value, zero or a positive value.Return Value none (无返回值)一、对int类型数组排序int num100;int cmp ( const void *a , const void *b )return *(int *)a - *(int *)b;qsort(num,100,sizeof(num0),cmp);二、对char类型数组排序(同int类型)char word100;int cmp( const void *a , const void *b )return *(char *)a - *(int *)b;qsort(word,100,sizeof(word0),cmp);三、对double类型数组排序double in100;int cmp( const void *a , const void *b )return *(double *)a *(double *)b ? 1 : -1;qsort(in,100,sizeof(in0),cmp);四、对结构体一级排序struct Sampledouble data;int other;s100/按照data的值从小到大将结构体排序int cmp( const void *a ,const void *b)return (*(Sample *)a).data (*(Sample *)b).data ? 1 : -1;qsort(s,100,sizeof(s0),cmp);五、对结构体二级排序struct Sampleint x;int y;s100;/按照x从小到大排序,当x相等时按照y从大到小排序int cmp( const void *a , const void *b )struct Sample *c = (Sample *)a;struct Sample *d = (Sample *)b;if(c-x != d-x) return c-x - d-x;else return d-y - c-y;qsort(s,100,sizeof(s0),cmp);六、对字符串进行排序struct Sampleint data;char str100;s100;/按照结构体中字符串str的字典顺序排序int cmp ( const void *a , const void *b )return strcmp( (*(Sample *)a)-str , (*(Sample *)b)-str );qsort(s,100,sizeof(s0),cmp);附加一个完整点的代码,对字符串二维数组排序:#include #include #include char s20011001;int cmp(const void *a, const void *b) return strcmp(char *)a,(char *)b);int main(
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 酒吧寻人活动方案
- 茶庄年会活动方案
- 高考会考试题及答案
- 高级防水考试题及答案
- 抚育技师考试题及答案
- 客户需求调研与问题解决方案
- 风景速描考试题及答案
- 我校招生宣传承诺书(3篇)
- 品牌宣传策略方案
- (正式版)DB15∕T 3355-2024 《规模化舍饲养羊主要疫病综合防治技术规程》
- 电梯维保员服务用语培训
- 审计案例分析单选题100道及答案解析
- 二年级上册《生态 生命 安全》教案
- 中交集团国考云题库
- 高龄患者PICC导管静脉血栓形成的个案分享课件
- 中国骨折内固定术后感染诊断与治疗专家共识
- 食品安全与日常饮食智慧树知到期末考试答案章节答案2024年中国农业大学
- 基础护理学第七版题附有答案
- (正式版)HGT 20593-2024 钢制化工设备焊接与检验工程技术规范
- 200个句子涵盖高中英语3500词汇
- 光线传媒公司章程
评论
0/150
提交评论