问问题描述
答精选答案

自定义一个比较函数,直接调用快排库函数qsort即可。举例如下:
//#include"stdafx.h
"//If the vc++6.0, with this line.#include
"stdio.h
"#include
"stdlib.h
"#include
"time.h
"struct ln{int data,score,math;}ss[100];int mycmp(const void *a, const void *b){//自定义比较函数return ((struct ln*)a)->data-((struct ln*)b)->data;//若要降序,-号前后变量交换}//data改为score或math就可按相应要素排序int main(void){//测试一下……int i;srand((unsigned)time(NULL));for(i=0;i<100;ss[i++].data=rand()%1000);qsort(ss,100,sizeof(struct ln),mycmp);for(i=0;i<100;printf(
"%4d
本文来自作者[甜甜的晓晓]投稿,不代表公众科技网立场,如若转载,请注明出处:https://www.cpst.net.cn/changshijingxuan/202609/1232391.html
评论列表(4条)
我是公众科技网的签约作者“甜甜的晓晓”!
希望本篇文章《C++ 快排》能对你有所帮助!
本站[公众科技网]内容主要涵盖:教育咨询,知识百科
本文概览:自定义一个比较函数,直接调用快排库函数qsort即可。举例如下://#include "stdafx.h"//If the vc++6.0, with this line.#include "stdio.h"#include "stdlib.h"#include "time.h"struct ln{int data,score,math;}ss[100];int mycmp(const void *a, const void *b){//自定义比较函数return ((struct ln*)a)->