C+±028-快排函数-2020-3-10
C++语言中排序的算法有很多种,系统也提供了一个函数qsort()可以实现快速排序。它包含在<stdlib.h>或 <cstdlib>
中,但要注意的是,qsort()的排序效果并不是最优。而且也不是稳定的排序。其原型如下:
void qsort
(void * base,size_t nmem,size_t size,int(* comp)(const void *,const void *));
它根据comp所指向的函数所提供的顺序对base所指向的数组进行排序,nmem为参数加排序的元素个数,size为每个元素所占的字节数。例如要对元素进行升序排列,则定义comp所指向的函数为:如果其第一个参数比第二个参数小,则返回一个小于0的值,反之则返回一个大于0的值,如果相等,则返回0。
要注意排序是从下标0开始,如果待排序的数组的元素是从1开始的话,调用qsort函数时一定要nmem+1,并根据你的排序需要对下标0的元素进行处理(比如从小到大排序由把下标0的元素赋值为无穷小,从大到小排序则把下标0的元素赋值为无穷大。这样才能保证下标0的元素不影响排序的结果。)
一、int 型排序
//int型qsort函数快排法
#include<iostream>
#include<cstdlib>
using namespace std;
int comp(const void *p,const void *q)//不可修改的参数
{
return (*(int *)p-*(int *)q);//转换两个指针为int后再比较
}
int comp2(const void *p,const void *q)//不可修改的参数
{
return (*(int *)q-*(int *)p);//转换两个指针为int后再比较
}
int main()
{
int i;
int array[]={6,8,2,9,1,0};
qsort(array,6,sizeof(int),comp);//数组名,元素数,元素大小,比较函数名
for(i=0;i<6;i++)//由小到大排序
{
cout<<array[i]<<' ';
}
cout<<endl;
qsort(array,6,sizeof(int),comp2);//数组名,元素数,元素大小,比较函数名
for(i=0;i<6;i++)//由大到小排序
cout<<array[i]<<' ';
return 0;
}
0 1 2 6 8 9
9 8 6 2 1 0
--------------------------------
Process exited with return value 0
Press any key to continue . . .
二、double型排序
//double型qsort函数快排法
#include<iostream>
#include<cstdlib>
using namespace std;
int cmp(const void * a,const void *b)//浮点数是不可能相等的
{
return((*(double *)a-*(double *)b>0)?1:-1);
}
int main()
{
double a[10];
for(int i=0;i<10;i++)
cin>>a[i];
qsort(a,10,sizeof(a[0]),cmp);
cout<<endl;
for(int i=0;i<10;i++)
cout<<a[i]<<endl;
return 0;
}
2
24
44
1
23
42
112
123123.231
123.1233333333
42
1
2
23
24
42
42
44
112
123.123
123123
--------------------------------
Process exited with return value 0
Press any key to continue . . .
三、字符串数组排序:
对于字符串(字典序排序)可以使用比较字符串大小的函数strcmp,strcmp的返回值正好符号comp函数的规则:
int comp_cstring(const void *a,const void *b)
{
return strcmp((char *)a,(char *)b);
}
假如有一个字符串数组 s[MAXN] [MAXT]
(有MAXN个字符串,每个字符串最长MAXT)那么像这样调用qsort函数就可以对这些字符串进行字典序排序了;
qsort(s,MAXN,sizeof(s[MAXN]),comp_cstring);
//对字符串数组排序
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
int cmp(const void *a,const void *b)
{
return strcmp((char *)a,(char *)b);
}
int main()
{
char a[10][5];//字符数组
for(int i=0;i<10;i++)
for(int j=0;j<5;j++)
cin>>a[i][j];
//qsort(a,10,sizeof(a[0]),cmp);
qsort(&a[3],5,sizeof(a[3]),cmp);//对部分字符串数组排序
cout<<endl;
for(int i=0;i<10;i++)
{
for(int j=0;j<5;j++)
cout<<a[i][j];
cout<<endl;
}
return 0;
}
Hello World
HELLO WORLD
hello world
hellowoold
hellowooldhellowolder
Hello
World
HELLO
WORLD
hello
hello
woold
world
hello
woold
--------------------------------
Process exited with return value 0
Press any key to continue . . .
如果要对数组进行部分排序,比如对一个s[n]的数组,要对其从s[i]开始的m个元素进行排序,只需要再第一个和第二个参数上进行一些修改:qsort[&s[i],m,sizeof(s[i],cmp)。
四、string排序
#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
int cmp(const void *a,const void *b)//升序排序
{
return strcmp((*(string *)a).data(),(*(string *)b).data());//升序排
return strcmp((*(string *)b).data(),(*(string *)a).data());//升序排
}
int main()
{
string *str=new string[10];
for(int i=0;i<10;i++)
cin>>str[i];
qsort(str,10,sizeof(string),cmp);
//qsort(&str[3],5,sizeof(str[3]),cmp); //对部分排序
cout<<endl;
for(int i=0;i<10;i++)
cout<<str[i]<<endl;
return 0;
}
hello world
hello World
Hello World
hello WORLD
HELLO WORLD
HELLO
Hello
WORLD
WORLD
World
World
hello
hello
hello
world
--------------------------------
Process exited with return value 0
Press any key to continue . . .
五、结构体排序(不稳定排序):
#include<iostream>
#include<cstdlib>
using namespace std;
struct node
{
double date;
int flag;
}
s[10];//结构体
int cmp(const void *a,const void *b)//浮点数是不可能相等的
{
return (((struct node *)a)->date>((struct node *)b)->date?1:-1);
}
int main()
{
for(int i=0;i<10;i++)
{
s[i].flag=i+1;
cin>>s[i].date;
}
qsort(s,10,sizeof(s[0]),cmp);
cout<<endl;
for(int i=0;i<10;i++)
cout<<s[i].flag<<" "<<s[i].date<<endl;
return 0;
}
231
123
123
123
12
3
4
5
6
1
10 1
6 3
7 4
8 5
9 6
5 12
2 123
4 123
3 123
1 231
--------------------------------
Process exited with return value 0
Press any key to continue . . .
六、结构体排序(稳定排序):
由于快排具有不稳定性,因此当data相等时,以flag为关键字进行排序。
#include<iostream>
#include<cstdlib>
using namespace std;
struct node
{
double date;
int flag;
}
s[10];//结构体
int cmp(const void *a,const void *b)//浮点数是不可能相等的
{ if(((struct node*)a)->date!=((struct node *)b)->date)//不同之处
return (((struct node *)a)->date>((struct node *)b)->date?1:-1);
else //不同之处
return (((struct node *)a)->flag-((struct node *)b)->flag);//不同之处
}
int main()
{
for(int i=0;i<10;i++)
{
s[i].flag=i+1;
cin>>s[i].date;
}
qsort(s,10,sizeof(s[0]),cmp);
cout<<endl;
for(int i=0;i<10;i++)
cout<<s[i].flag<<" "<<s[i].date<<endl;
return 0;
}
1
1
2
2
321
123
2
5
9
1
1 1
2 1
10 1
3 2
4 2
7 2
8 5
9 9
6 123
5 321
--------------------------------
Process exited with return value 0
Press any key to continue . . .
七、二维数组排序
其实字符串数组就已经是一个二维数组了,所以在调用qsort函数的时候与字符串数组类似。
//二维数组排序
#include<iostream>
#include<cstdlib>
using namespace std;
int cmp(const void *a,const void *b)
{
if(((int *)a)[0]-((int *)b)[0]==0)//如果第一元素相等
return((int *)a)[1]-((int *)b)[1];//比较第二元素
else
return ((int *)a)[0]-((int *)b)[0];//否则比较第一元素
}
int main()
{
int s[10][2];
for(int i=0;i<10;i++)
cin>>s[i][0]>>s[i][1];
qsort(s,10,sizeof(s[0]),cmp);
cout<<endl;
for(int i=0;i<10;i++)
cout<<s[i][0]<<" "<<s[i][1]<<endl;
return 0;
}
12
32
12
124
35
34
23
231
123
123
123
2
2
42
2
23
23
1
2
21
2 21
2 23
2 42
12 32
12 124
23 1
23 231
35 34
123 2
123 123
--------------------------------
Process exited with return value 0
Press any key to continue . . .