qsort()函數用法
功能:該函數為庫函數,使用快速排序例程進行排序。
頭文件:#include<stdlib.h>
原型:void qsort(void *base, int nelem, int width, int (*fcmp)(const void *,const void *))
參數: 1.待排序數組首地址
2.數組中待排序的元素個數
3.個元素的所占空間的大小
4.指向函數的指針,用于確定排序的順序
qsort可對各類數組進行排序,主要分為七大類。qsort要求提供一個比較函數(即第四個參數函數指針),是為了做到通用性更好一些。
以下是對各類的討論(本文默認排序都是有小到大,若需從大到小可改變函數指針兩參數的順序):
一、對int類型數組排序:
int num[100];
需要自己構造的判斷函數:
int cmp ( const void *a , const void *b)
{
return *(int *)a - *(int *)b; //順序有小到大,如需從大到小將兩數字顛倒運算
}
調用庫函數qsort:
qsort(num, 100 ,sizeof(num[0]), cmp);
二、對char類型數組排序:
char s[100];
需要自己構造的判斷函數:
int cmp ( const void *a , const void *b)
{
return *(char *)a - *(char *)b;
}
調用庫函數qsort:
qsort(s, 100 ,sizeof(s[0]), cmp);
三、對double類型數組排序:
double num[100];
需要自己構造的判斷函數:
int cmp ( const void *a , const void *b)
{
return *(double *)a > *(double *)b ? 1 : -1; //特別注意!!!
}
調用庫函數qsort:
qsort(num, 100 ,sizeof(num[0]), cmp);
四、對結構體一級排序:
struct In{
double data;
int other;
}s[100];
需要自己構造的判斷函數:
int cmp ( const void *a , const void *b)
{
return (*(struct In *)a).data > (*(struct In *)b).data ? 1 : -1; //特別注意!!!
}
調用庫函數qsort:
qsort(s, 100 ,sizeof(s[0]), cmp);
五、對結構體二級排序:
struct In{
int x;
int y;
}s[100]; //按照 x 從小到大排序, x 相等的時候按照 y 從大到小排序
需要自己構造的判斷函數:
int cmp ( const void *a , const void *b)
{
struct In * c = (In *)a;
struct In * d = (In *)b;
if ( c-> x != d-> x )
return c-> x - d-> x ;
else
return d-> y - c-> y ;
}
調用庫函數qsort:
qsort(s, 100 ,sizeof(s[0]), cmp);
六、對字符串進行排序:
struct In{
int x;
char str[100];
}s[100]; //按照結構體中字符串str的字典的順序進行排序
需要自己構造的判斷函數:
int cmp ( const void *a , const void *b)
{
return strcmp( (*(struct In *)a).str > (*(struct In *)b).str );
}
調用庫函數qsort:
qsort(s, 100 ,sizeof(s[0]), cmp);
七、計算幾何中求凸包的cmp
int cmp( const void* a, const void* b) //重點是cmp函數,把除了1點以外的所有點,旋轉角度排序
{
struct point *c = (point *)a;
struct point *d = (point *)b;
if ( calc(*c, *d, p[1]) < 0)
return 1;
else if ( !calc(*c, *d, p[1]) && dis(c->x, c->y, p[1].y ) < dis(d->x, d->y, p[1].x, p[1].y )) //如果在一條直線上,則把遠的放在前面
return 1;
else
return -1;
}
具體應用:
下面是我使用qsort排序一個整型數組,一個浮點型數組,一個字符串數組的例子:
#include<stdio.h>#include<string.h>typedef struct In{ int data; char str[100];}s[5];int cmp_int(const void *a, const void *b) //由大到小排序 { return *(int*)a - *(int*)b;}int cmp_double(const void *a, const void *b) //由大到小排序{ return *(double*)a > *(double*)b ? 1 : -1;}int cmp_str(const void *a, const void *b) //用char**對字符串數組進行排序 { return strcmp(*(char**)a, *(char**)b);}int cmp_str2(const void *a, const void *b) //用結構體對字符串數組進行排序 { return strcmp((*(struct In*)a).str, (*(struct In*)b).str);}int main(){ int i = 0; int arr1[10] = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 10 }; double arr2[10] = { 6.2, 8.4, 5.2, 9.5, 4.3, 2.1, 7.4, 0.1, 5.5, 6.6 }; char* str[5] = { "mno", "def", "abc", "ghi", "jkl" }; struct In s[5] = { { 1, "mno" }, { 2, "def" }, { 3, "abc" }, { 4, "ghi" }, { 5, "jkl" } }; qsort(arr1, 10, sizeof(int), cmp_int); //對整數數組排序 qsort(arr2, 10, sizeof(double), cmp_double); //對浮點型數組排序 qsort(str, 5, sizeof(char*), cmp_str); //對結構體字符串進行排序(方法1) qsort(s, 5, sizeof(s[0]), cmp_str2); //對結構體字符串進行排序(方法2) for (i = 0; i<10; i++) { PRintf("%d ", arr1[i]); } printf("/n"); for (i = 0; i<10; i++) { printf("%.1lf ", arr2[i]); } printf("/n"); for (i = 0; i<5; i++) //用char**對字符串數組進行排序打印 { printf("%s ", str[i]); } printf("/n"); for (i = 0; i<5; i++) //用結構體對字符串數組進行排序打印 { printf("%s ", s[i].str); } return 0;}運行結果:
新聞熱點
疑難解答