qsort

功 能: 使用快速排序例程进行排序
头文件:stdlib.h
用 法: void qsort(void *base,int nelem,int width,int (*fcmp)(const void *,const void *));
参数: 
1 待排序数组首地址
2 数组中待排序元素数量
3 各元素的占用空间大小
4 (比较函数)指向函数的指针,用于确定排序的顺序
比如:对一个长为1000的数组进行排序时,int a[1000]; 那么base应为a,num应为 1000,width应为 sizeof(int),comp函数随自己的命名。
qsort(a,1000,sizeof(int),comp);
其中comp函数应写为:
1
2
3
4
int comp ( const void *a, const void *b )
{
    return * ( int * ) a - * ( int * ) b;
}
上面是由小到大排序return *(int *)b-*(int *)a; 为由大到小排序



posted @ 2016-03-16 21:42  copperface  阅读(193)  评论(0编辑  收藏  举报