引用#include<stdlib.h>頭文件
qsort()括號里面有4個參數
第一個參數是將要排序的數組名array;
第二個參數是將要排序的數量n;
第三個參數是每個要排序的參數的大小xizeof(array[o]);
第四個參數是自己寫的一個比較函數comp;
若排序的是int類型的數組
升序:
int comp(const void*a,const void*b)//用來做比較的函數。
{
return *(int*)a - *(int*)b;
}
降序:
int comp(const void*a,const void*b)//用來做比較的函數。
{
return *(int*)b - *(int*)a; //降序
}
若排序的是結構體數組,假定結構體node
typedef struct{
int a; int b;
}node;
qsort(a,n,sizeof(node),comp)
int comp(const void*x, const void*y)
{
return ((*(node*)y).a) - ((*(node*))x).a);
}