sort与qsort的应用

来源:互联网 发布:淘宝访问深度怎么计算 编辑:程序博客网 时间:2024/04/28 17:15

一、qsort

1.int

[cpp] view plaincopy
  1. int num[100];   
  2.   
  3. int cmp ( const void *a , const void *b )   
  4. {   
  5. return *(int *)a - *(int *)b; //升序排列。if(a>b) return true  
  6. }   
  7.   
  8. qsort(num,100,sizeof(num[0]),cmp);   


2.char

[html] view plaincopy
  1. char word[100];   
  2. int cmp( const void *a , const void *b )  
  3. {   
  4.     return *(char *)a - *(int *)b;   
  5. }   
  6. qsort(word,100,sizeof(word[0]),cmp);   


3.double

[cpp] view plaincopy
  1. double in[100];   
  2. int cmp( const void *a , const void *b )   
  3. {   
  4.     return *(double *)a > *(double *)b ? 1 : -1;   
  5. }   
  6. qsort(in,100,sizeof(in[0]),cmp);   


4.结构体

 

[cpp] view plaincopy
  1. struct Nation  
  2. {  
  3.     int jin;  
  4.     int jiang;  
  5.     int people;  
  6.     int index;  
  7.     int num[6];  
  8. };  
  9.   
  10.   
  11. int mode;  
  12. int comp(const void *A,const void* B)  
  13. {  
  14.     struct Nation* a=(Nation*)A;  
  15.     struct Nation* b=(Nation*)B;  
  16.   
  17.   
  18.     switch(mode)//都是降序  
  19.     {  
  20.     case 1:   
  21.         return b->jin-a->jin;  
  22.     case 2:  
  23.         return b->jiang-a->jiang;  
  24.     case 3:  
  25.         return b->jin*a->people-a->jin*b->people;  
  26.     case 4:  
  27.         return b->jiang*a->people-a->jiang*b->people;  
  28.     }  
  29. }  
  30. qsort(s,100,sizeof(s[0]),cmp);   


5、对字符串进行排序

[cpp] view plaincopy
  1. char str[100][100];  
  2. int cmp(const void* a,const void* b )  
  3. {  
  4.            return strcmp((char *)a,(char*)b);  
  5. }  
  6. qsort(str,n,sizeof(str[0]),cmp);  

 

值得注意的是,上面的n,很有可能你会误认为是100,这里实际应该是你要排序的个数,比如说你实际上只有str[0],str[1],str[2]这三个字符串要排序,那么n就应该是3,而不是100;

 

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);

 

二、sort

方法和qsort类似,std::sort是一个改进版的qsort. std::sort函数优于qsort的一些特点:对大数组采取9项取样,更完全的三路划分算法,更细致的对不同数组大小采用不同方法排序。

[cpp] view plaincopy
  1. bool compare(int a,int b)  
  2. {  
  3.   return a>b; //降序排列,如果改为return a<b,则为升序  
  4. }  


[cpp] view plaincopy
  1. struct Nation  
  2. {  
  3.     int jin;  
  4.     int jiang;  
  5.     int people;  
  6.     int index;  
  7.     int num[6];  
  8. }c[100];  
  9.   
  10. int mode;  
  11. bool comp(const Nation &a,const Nation& b)  
  12. {  
  13.     switch(mode)\\都是降序  
  14.     {  
  15.     case 1:   
  16.         return a.jin>b.jin;  
  17.     case 2:  
  18.         return a.jiang>b.jiang;  
  19.     case 3:  
  20.         return a.jin*b.people>b.jin*a.people;  
  21.     case 4:  
  22.         return a.jiang*b.people>b.jiang*a.people;  
  23.     }  
  24. }  
  25.   
  26.   
  27. sort(c,c+m,comp);  



诺~同是降序,sort的comp函数是a>b而qsort的函数是return b-a;

最后,还是推荐用sort!

 =================================================================================================================

具体来说可以用下面这个例子说明两个实现相同功能的sort和qsort

[cpp] view plaincopy
  1. #include "iostream"  
  2. #include "algorithm"  
  3. #include "time.h"  
  4. using namespace std;  
  5.   
  6. int comp(const void *a,const void *b)  
  7. {  
  8.     return *(int *)b-*(int *)a;  
  9. }  
  10.   
  11. bool cmp(int a,int b)  
  12. {  
  13.     return a>b;  
  14. }  
  15.   
  16. int main()  
  17. {  
  18.     int n,m,i,j;  
  19.     int a[1000];  
  20.     srand((unsigned) time(NULL));  
  21.     n=10;  
  22.     printf("origin:\t");  
  23.     for(i=0;i<n;i++)  
  24.     {  
  25.         a[i]=rand()%1000;  
  26.         printf("%d ",a[i]);  
  27.     }  
  28.     cout<<endl;  
  29.     qsort(a,10,sizeof(a[0]),comp);  
  30.     printf("qsort:\t");  
  31.     for (i=0;i<n;i++)  
  32.     {  
  33.         printf("%d ",a[i]);  
  34.     }  
  35.     printf("\nsort:\t");  
  36.     sort(a,a+9,cmp);  
  37.     for (i=0;i<n;i++)  
  38.     {  
  39.         printf("%d ",a[i]);  
  40.     }  
  41.     cout<<endl;  

0 0
原创粉丝点击