C库函数qsort七种使用方法示例(转自别处,仅为收藏) sort排序
2011-07-08 12:11
961 查看
七种qsort排序方法<本文中排序都是采用的从小到大排序>
一、对int类型数组排序
C++代码
int num[100];
Sample:
int cmp ( const void *a , const void *b )
{
return *(int *)a – *(int *)b;
}
qsort(num,100,sizeof(num[0]),cmp);
二、对char类型数组排序(同int类型)
C++代码
char word[100];
Sample:
int cmp( const void *a , const void *b )
{
return *(char *)a – *(char*)b;
}
qsort(word,100,sizeof(word[0]),cmp)
三、对double类型数组排序(特别要注意)
C++代码
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);
四、对结构体一级排序
C++代码
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);
五、对结构体二级排序
C++代码
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);
六、对字符串进行排序
C++代码
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);
七、计算几何中求凸包的cmp
C++代码
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].x,p[1].y) < dis(d->x,d->y,p[1].x,p[1].y))
//如果在一条直线上,则把远的放在前面
return 1; else return -1;
}
PS: 其中的qsort函数包含在的头文件里,strcmp包含在的头文件里
有关sort:
方法和qsort类似,std::sort是一个改进版的qsort. std::sort函数优于qsort的一些特点:对大数组采取9项取样,更完全的三路划分算法,更细致的对不同数组大小采用不同方法排序。
bool compare(int a,int b)
{
return a>b; //降序排列,如果改为return a<b,则为升序
}
诺~同是降序,sort的comp函数是a>b而qsort的函数是return b-a;
最后,还是推荐用sort!
=================================================================================================================
具体来说可以用下面这个例子说明两个实现相同功能的sort和qsort
一、对int类型数组排序
C++代码
int num[100];
Sample:
int cmp ( const void *a , const void *b )
{
return *(int *)a – *(int *)b;
}
qsort(num,100,sizeof(num[0]),cmp);
二、对char类型数组排序(同int类型)
C++代码
char word[100];
Sample:
int cmp( const void *a , const void *b )
{
return *(char *)a – *(char*)b;
}
qsort(word,100,sizeof(word[0]),cmp)
三、对double类型数组排序(特别要注意)
C++代码
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);
四、对结构体一级排序
C++代码
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);
五、对结构体二级排序
C++代码
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);
六、对字符串进行排序
C++代码
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);
七、计算几何中求凸包的cmp
C++代码
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].x,p[1].y) < dis(d->x,d->y,p[1].x,p[1].y))
//如果在一条直线上,则把远的放在前面
return 1; else return -1;
}
PS: 其中的qsort函数包含在的头文件里,strcmp包含在的头文件里
有关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); 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,i; 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+n,cmp); for (i=0;i<n;i++) { printf("%d ",a[i]); } cout<<endl; return 0; }
相关文章推荐
- C++ 排序函数 sort(),qsort()的使用方法
- go语言中排序sort的使用方法示例
- C++ 排序函数 sort(),qsort()的使用方法
- C++ 排序函数 sort(),qsort()的使用方法
- C++ 排序函数 sort(),qsort()的使用方法
- C++ 排序函数 sort(),qsort()的使用方法
- C++ 排序函数 sort(),qsort()的使用方法
- C++ 排序函数 sort(),qsort()的使用方法
- 使用Collections.sort方法对list排序的两种方法
- 浅谈JavaScript--Array数组sort()排序方法与自定义比较器的使用
- JavaScript 使用sort()方法来给数组排序
- 拖拽排序ListViewdrag-sort-listview使用方法
- PHP 使用header函数设置HTTP头的示例方法 表头
- Java排序方法sort的使用详解
- JS中的数组的sort方法使用示例
- PHP 使用header函数设置HTTP头的示例方法 表头(转)
- C#泛型-使用委托的Sort排序方法
- Babelfish(6.1.2)(sort结构体排序)(sscanf()基本使用方法)(二分法)
- Collection.sort自定义排序的使用方法