C语言快速排序函数用法(qsort)
本文实例为大家分享了C语言快排函数用法,供大家参考,具体内容如下
#include#include #include structstudent { intid; charname[12]; charsex; }; intcompare(constvoid*a,constvoid*b)//基本数据类型排序 { return*(char*)a-*(char*)b;//从小到大 //取值//强转为相应类型的指针!! } intcompare_struct(constvoid*a,constvoid*b) { return(*(structstudent*)a).id-((structstudent*)b)->id; //注意优先级诶!//否则报错在非结构体中。。。 } intcompare_struct_duoji(constvoid*a,constvoid*b)//多级排序 { structstudentstudent_a=*(structstudent*)a; structstudentstudent_b=*(structstudent*)b; if(student_a.id==student_b.id) { returnstudent_a.sex-student_b.sex; } else { returnstudent_a.id-student_b.id; } } voidmain() { //*************char型************* chara[5]="hello"; qsort(a,5,sizeof(a[0]),compare); //元素个数//元素大小//函数指针 inti; for(i=0;i<5;i++) printf("%c",a[i]); printf("\n"); //************struct型************ structstudente[4]={{100,"chen",'m'},{100,"li",'f'},\ {70,"wang",'f'},{100,"zhang",'m'}}; qsort(e,4,sizeof(e[1]),compare_struct_duoji); for(i=0;i<4;i++) printf("%d%s%c\n",e[i].id,e[i].name,e[i].sex); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。