C语言有自有的qsort函数。
来自百科,收藏
1
2
3
4
|
int
comp(
const
void
*a,
const
void
*b)
{
return
*(
int
*)a-*(
int
*)b;
}
|
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;
}
|
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;
}