sort 与 qsort

C语言有自有的qsort函数。


来自百科,收藏


函数简介

编辑
功 能: 使用 快速排序例程进行排序
头文件:stdlib.h
用 法: void qsort(void *base,int nelem,int width,int (*fcmp)(const void *,const void *));
参数: 1 待排序 数组首地址
2 数组中待排序元素数量
3 各元素的占用空间大小
4 指向函数的 指针,用于确定排序的顺序

用法

编辑
其中base是排序的一个集合 数组,num是这个数组元素的个数,width是一个元素的大小,comp是一个比较函数。
比如:对一个长为1000的数组进行排序时,int a[1000]; 那么base应为a,num应为 1000,width应为 sizeof(int),comp函数随自己的命名。
qsort(a,1000,sizeof(int),comp);
其中comp函数应写为:
1
2
3
4
int  comp( const  void *a, const  void *b)
{
return  *( int *)a-*( int *)b;
}
上面是由小到大排序, return *(int *)b - *(int *)a; 为由大到小排序。
MSDN:
The qsort function implements a quick-sort algorithm to sort an array of num elements, each of width bytes. The argument base is a pointer to the base of the array to be sorted. qsort overwrites this array with the sorted elements. The argument compare is a pointer to a user-supplied routine that compares two array elements and returns a value specifying their relationship. qsort calls the compare routine one or more times during the sort, passing pointers to two array elements on each call:
以下为compare函数原型 //comp
compare( (void *) & elem1, (void *) & elem2 );
Compare 函数的返回值
描述
< 0
elem1将被排在elem2前面
0
elem1 等于 elem2
> 0
elem1 将被排在elem2后面
对一维数组的排序实例(从小到大排序):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<stdio.h>
#include<stdlib.h>
int  comp( const  void *a, const  void *b)
{
return  *( int *)a-*( int *)b;
}
int  main()
{
int  i=0;
int  *array;
int  n;
scanf ( "%d" ,&n);
array=( int *) malloc (n* sizeof ( int ));
 
for (;i<n;i++)
{
scanf ( "%d" ,(array+i));
}
qsort (array,n, sizeof ( int ),comp);
for (i=0;i<n;i++)
{
printf ( "%d\t" ,array[i]);
}
return  0;
}
对一个 二维数组进行排序:
int a[1000][2]; 其中按照a[0]的大小进行一个整体的排序,其中a[1]必须和a[0]一起移动交换。//即第一行和第二行(a[0]和a[1]分别代表第一行和第二行的首地址)。使用库函数排序的代码量并不比用冒泡排序法小,但速度却快很多。
1
2
3
4
5
6
7
qsort (a,1000, sizeof ( int )*2,comp);
 
int  comp( const  void *a, const  void *b)
 
{
return (( int *)a)[0]-(( int *)b)[0];
}
对字符串进行排序
1
2
3
4
5
6
7
8
9
10
int  Comp( const  void *p1, const  void *p2)
{
return  strcmp (( char *)p2,( char *)p1);
}
int  main()
{
char  a[MAX1][MAX2];
initial(a);
qsort (a,lenth, sizeof (a[0]),Comp);
//lenth为数组a的长度
按结构体中某个关键字排序(对结构体一级排序):
1
2
3
4
5
6
7
8
9
10
structNode
{
double  data;
int  other;
}s[100];
int  Comp(constvoid*p1,constvoid*p2)
{
return (*(Node*)p2).data>(*(Node*)p1).data?1:-1;
}
qsort (s,100, sizeof (s[0]),Comp);
按结构体中多个关键字排序(对结构体多级排序)[以二级为例]:
1
2
3
4
5
6
7
8
9
10
11
12
13
struct  Node
{
int  x;
int  y;
}s[100];
//按照x从小到大排序,当x相等时按y从大到小排序
int  Comp( const  void *p1, const  void *p2)
{
struct  Node*c=(Node*)p1;
struct  Node*d=(Node*)p2;
if (c->x!=d->x)returnc->x-d->x;
else  return  d->y-c->y;
}
对结构体中字符串进行排序:
1
2
3
4
5
6
7
8
9
10
11
struct  Node
{
int  data;
char  str[100];
}s[100];
//按照结构体中字符串str的字典序排序
int  Comp( const  void *p1, const  void *p2)
{
return  strcmp ((*(Node*)p1).str,(*(Node*)p2).str);
}
qsort (s,100, sizeof (s[0]),Comp);


Sort函数包含在头文件为#include<algorithm>的c++标准库中,调用标准库里的排序方法可以不必

知道其内部是如何实现的,只要出现我们想要的结果即可! II)Sort函数有三个参数: 

(1)第一个是要排序的数组的起始地址。 
(2)第二个是结束的地址(最后一位要排序的地址) 
(3)第三个参数是排序的方法,可以是从大到小也可是从小到大,还可以不写第三个参数,此时默

认的排序方法是从小到大排序。 Sort函数使用模板: Sort(start,end,,排序方法) 
下面就具体使用sort()函数结合对数组里的十个数进行排序做一个说明!

例一:sort函数没有第三个参数,实现的是从小到大 

#include<iostream> 

#include<algorithm> 

using namespace std; 

int main() { 
 int a[10]={9,6,3,8,5,2,7,4,1,0};

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

cout<<a[i]<<endl;

sort(a,a+10);

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

 cout<<a[i]<<endl;

 return 0; } 

例二 
通过上面的例子,会产生疑问:要实现从大到小的排序肿么办?  这就如前文所说需要在sort()

函数里的第三个参数里做文章了,告诉程序我要从大到小排序! 
需要加入一个比较函数 complare(),此函数的实现过程是这样的

bool complare(int a,int b) 


 return a>b;


这就是告诉程序要实现从大到小的排序的方法! 

#include<iostream> 

#include<algorithm> 

using namespace std;

bool complare(int a,int b) 


 return a>b; 

int main() { 
 int a[10]={9,6,3,8,5,2,7,4,1,0};

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

 cout<<a[i]<<endl; 
 sort(a,a+10,complare);//在这里就不需要对complare函数传入参数了,//这是规则 
 for(int i=0;i<10;i++)

 cout<<a[i]<<endl;

 return 0;

}

例三: 
通过上面例一、二的方法虽然实现了从大到小和从大到小的排序,这样做还是有点麻烦,因为

还需要自己编写告诉程序进行何种排序的函数,c++标准库强大的功能完全可以解决这种麻烦。 
Sortt函数的第三个参数可以用这样的语句告诉程序你所采用的排序原则 
less<数据类型>()//从小到大排序 greater<数据类型>()//从大到小排序

结合本例子,这样的就可以完成你想要的任何一种排序原则了 

#include<iostream> 

#include<algorithm> 

using namespace std;

int main() { 
 int a[10]={9,6,3,8,5,2,7,4,1,0};

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

 cout<<a[i]<<endl;

sort(a,a+10,less<int>());

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

 cout<<a[i]<<endl;

 return 0; 
}


#include<iostream>

#include<algorithm>

using namespace std;

int main() { 
 int a[10]={9,6,3,8,5,2,7,4,1,0};

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

 cout<<a[i]<<endl;

 sort(a,a+10,greater<int>());

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

 cout<<a[i]<<endl;

 return 0;

}


例四:利用sort函数还可以实现对字符的排序,排序方法大同小异,下面就把程序范例展示一下 

#include<iostream> 

#include<algorithm> 

using namespace std;

int main() { 
 char a[11]="asdfghjklk";

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

 cout<<a[i]<<endl; 
 sort(a,a+10,greater<char>());

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

 cout<<a[i]<<endl;

 return 0;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值