C++ 快排

问问题描述

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)->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

赞 (0)

发表回复

本站作者后才能评论

评论列表(4条)

  • 甜甜的晓晓
    甜甜的晓晓 2026年09月30日

    我是公众科技网的签约作者“甜甜的晓晓”!

  • 甜甜的晓晓
    甜甜的晓晓 2026年09月30日

    希望本篇文章《C++ 快排》能对你有所帮助!

  • 甜甜的晓晓
    甜甜的晓晓 2026年09月30日

    本站[公众科技网]内容主要涵盖:教育咨询,知识百科

  • 甜甜的晓晓
    甜甜的晓晓 2026年09月30日

    本文概览:自定义一个比较函数,直接调用快排库函数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)->

联系我们

联系:143 0457 151

工作时间:周一至周五,9:30-18:30,节假日休息

关注我们