Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2378102
  • 博文数量: 298
  • 博客积分: 7876
  • 博客等级: 准将
  • 技术积分: 5500
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-23 13:39
文章存档

2013年(2)

2012年(142)

2011年(154)

分类: C/C++

2011-08-14 16:22:09

排序之qsort函数

qsort函数是C语言库提供的快速排序的库函数,位于头文件在一般应用的时候我们应该尽量选择使用库函数,因为库函数一般都是经过优化总结出来的C代码,这样也可以少写大篇幅的代码。

1.qsort函数之int型排序

注意C语言里没有布尔类型,一般为0值为假,非零值为真。

int array[100];

int cmp ( const  void   *a , const   void   *b   )     

{     

return   *(int   *)a   -   *(int   *)b;     

}  

qsort(array,100,sizeof(array[0]),cmp);

测试程序:

#include 

#include 

#include 

int cmp ( const  void   *a , const   void   *b   )     

{     

return   *(int   *)a   -   *(int   *)b;     

}  

int main(int argc, char **argv)

{

int array[100];

int i,j;

int p[2];

int key;

srand(time(0));

for(i = 0; i< 100; i++)

{

array[i] = rand()%100;

}

qsort(array,100,sizeof(array[0]),cmp);

for(i = 0; i< 10; i++)

{

for(j=0; j<10; j++)

{

printf(" %d ", array[i*10+j]);

}

printf("\n");

}

return 0;

}

程序结果:

 0  1  1  2  4  6  7  7  8  8

 9  9  9  13  14  15  16  18  18  18

 18  19  23  23  24  24  24  26  26  26

 28  28  29  29  31  34  36  37  37  37

 37  39  39  39  40  40  42  42  44  46

 47  48  48  49  51  54  55  57  58  59

 59  60  63  63  63  64  65  65  65  66

 72  76  76  77  77  80  81  81  82  85

 86  87  87  87  89  89  89  90  90  90

 92  93  93  95  96  96  97  97  98  98

请按任意键继续. . .、、

2.qsort函数之char型排序(同int)

  char   word[100];     

  Sample:     

  int   cmp(const   void   *a , const   void   *b   )     

  {     

 return   *(char   *)a   -   *(int   *)b;     

  } 

3.qsort函数之double型排序(特别注意

  double   in[100];     

    

  int   cmp(   const   void   *a   ,   const   void   *b   )     

  {     

   return   *(double   *)a   >   *(double   *)b   ?   1   :   -1;     

  }     

    

  qsort(in,100,sizeof(in[0]),cmp); 

4.qsort函数之结构体型一级排序

 struct   In     

  {     

   double   data;     

   int   other;     

  }s[100]     

    

  //按照data的值从小到大将结构体排序,关于结构体内的排序关键数据data的类型可以很多种,参考上面的例子写     

    

  int   cmp(   const   void   *a   ,const   void   *B)     

  {     

   return   (*(In   *)a)->data   >   (*(In   *)B)->data   ?   1   :   -1;     

  }     

    

  qsort(s,100,sizeof(s[0]),cmp);     

5.qsort函数之结构体型二级排序

 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(s,100,sizeof(s[0]),cmp);     

6.qsort函数之字符串排序

 struct   In     

  {     

  int   data;     

  char   str[100];     

  }s[100];     

    

  //按照结构体中字符串str的字典顺序排序     

    

  int   cmp   ( const   void   *a   ,   const   void   *b   )     

  {     

   return   strcmp(  (*(In   *)a)->str(*(In   *)B)->str   );     

  }     

    

  qsort(s,100,sizeof(s[0]),cmp);     

阅读(1941) | 评论(0) | 转发(2) |
0

上一篇:数据结构之AVL树

下一篇:图之基础

给主人留下些什么吧!~~