昨天做了一题,一直wa;改了俩小时 终于想把qsort换成sort试试,居然ac了……我什么也不想说了!进入正题,说说sort和qsort的区别和应用。
=================================================================================================================
一、qsort
1.int
int num[100]; int cmp ( const void *a , const void *b ) { return *(int *)a - *(int *)b; //升序排列。if(a>b) return true } qsort(num,100,sizeof(num[0]),cmp);
2.char
char word[100]; int cmp( const void *a , const void *b ) { return *(char *)a - *(int *)b; } qsort(word,100,sizeof(word[0]),cmp);
3.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.结构体
struct Nation { int jin; int jiang; int people; int index; int num[6]; }; int mode; int comp(const void *A,const void* B) { struct Nation* a=(Nation*)A; struct Nation* b=(Nation*)B; switch(mode)//都是降序 { case 1: return b->jin-a->jin; case 2: return b->jiang-a->jiang; case 3: return b->jin*a->people-a->jin*b->people; case 4: return b->jiang*a->people-a->jiang*b->people; } } qsort(s,100,sizeof(s[0]),cmp);
5、对字符串进行排序
char str[100][100]; int cmp(const void* a,const void* b ) { return strcmp((char *)a,(char*)b); } 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项取样,更完全的三路划分算法,更细致的对不同数组大小采用不同方法排序。
bool compare(int a,int b) { return a>b; //降序排列,如果改为return a<b,则为升序 }
struct Nation { int jin; int jiang; int people; int index; int num[6]; }c[100]; int mode; bool comp(const Nation &a,const Nation& b) { switch(mode)\\都是降序 { case 1: return a.jin>b.jin; case 2: return a.jiang>b.jiang; case 3: return a.jin*b.people>b.jin*a.people; case 4: return a.jiang*b.people>b.jiang*a.people; } } sort(c,c+m,comp);
诺~同是降序,sort的comp函数是a>b而qsort的函数是return b-a;
最后,还是推荐用sort!
=================================================================================================================
具体来说可以用下面这个例子说明两个实现相同功能的sort和qsort
#include "iostream" #include "algorithm" #include "time.h" using namespace std; int comp(const void *a,const void *b) { return *(int *)b-*(int *)a; } bool cmp(int a,int b) { return a>b; } int main() { int n,m,i,j; int a[1000]; srand((unsigned) time(NULL)); n=10; printf("origin:\t"); for(i=0;i<n;i++) { a[i]=rand()%1000; printf("%d ",a[i]); } cout<<endl; qsort(a,10,sizeof(a[0]),comp); printf("qsort:\t"); for (i=0;i<n;i++) { printf("%d ",a[i]); } printf("\nsort:\t"); sort(a,a+9,cmp); for (i=0;i<n;i++) { printf("%d ",a[i]); } cout<<endl; }