自定义一个比较函数,直接调用快排库函数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",ss[i++].data));
printf("\n");
return 0;
}