全站数据
8 4 2 0 5 8 1

C++ 快排

注册消防工程师 | 简单学习,快乐成才!         
问题更新日期:2024-10-15 13:58:39

问题描述

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

随机推荐