排序(qsort sort的使用)

时间:2023-03-09 05:26:29
排序(qsort sort的使用)

前情:因平常写代码是常将比较函数弄混(写好了排序还要确认一下-.-!),还是写篇博客,方便以后查阅

C语言qsort函数int类型数组排序:

 #include "stdio.h"
#include "stdlib.h"
#define N 1005
int a[N]; //此比较函数让数组从小到大排列
int cmp(const void *a,const void *b){ return *(int *)a-*(int *)b; } int main()
{
int i,n;
scanf("%d",&n);
for(i=; i<n; i++) scanf("%d",&a[i]);
qsort(a,n,sizeof(a[]),cmp);
for(i=; i<n; i++) printf("%d ",a[i]);
printf("\n");
return ;
}

C++中sort函数int类型数组排序:

 #include "cstdio"
#include "algorithm"
using namespace std;
#define N 1005
int a[N];
//sort函数不写比较函数的话,默认是从小到大排序的 int main()
{
int i,n;
scanf("%d",&n);
for(i=; i<n; i++) scanf("%d",&a[i]);
sort(a,a+n); //将地址从a开始到a+n(不包括地址a+n对应元素)的所有数据进行排序
for(i=; i<n; i++) printf("%d ",a[i]);
printf("\n");
return ;
}

C语言qsort函数char类型数组排序:

 #include "stdio.h"
#include "string.h"
#include "stdlib.h"
#define N 1005
char str[N]; //此比较函数让数组从小到大排列
int cmp(const void *a,const void *b){ return *(char *)a-*(char *)b; } int main()
{
int len;
scanf("%s",str);
len = strlen(str);
qsort(str,len,sizeof(str[]),cmp);
puts(str);
return ;
}

C++中sort函数char类型数组排序:

 #include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
#define N 1005
char str[N]; int main()
{
int len;
scanf("%s",str);
len = strlen(str);
sort(str,str+len); //默认也是按ascll码从小到大排序
puts(str);
return ;
}

C语言qsort函数double类型数组排序:

 #include "stdio.h"
#include "string.h"
#include "stdlib.h"
#define N 1005
double a[N]; //此比较函数让数组从小到大排列
int cmp(const void *a,const void *b)
{
return *(double *)a > *(double *)b ? :-;
} int main()
{
int i,n;
scanf("%d",&n);
for(i=; i<n; i++) scanf("%lf",&a[i]);
qsort(a,n,sizeof(a[]),cmp);
for(i=; i<n; i++) printf("%.1lf ",a[i]);
printf("\n");
return ;
}

C++中sort函数double类型数组排序:

 #include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
#define N 1005
double a[N]; //从小到大排序
bool cmp(double a,double b)
{
return a<b;
} int main()
{
int i,n;
scanf("%d",&n);
for(i=; i<n; i++) scanf("%lf",&a[i]);
sort(a,a+n,cmp);
for(i=; i<n; i++) printf("%.1lf ",a[i]);
printf("\n");
return ;
}

C语言中qsort函数结构体二级排序:

 #include "stdio.h"
#include "string.h"
#include "stdlib.h"
#define N 1005 typedef struct
{
int x,y;
}Point;
Point a[N];
//先按结构体中x从小到大排序,x相等的按y从大到小排序
int cmp(const void *a,const void *b)
{
Point *c = (Point *)a;
Point *d = (Point *)b;
if(c->x != d->x)
return c->x - d->x;
return d->y - c->y;
}
int main()
{
int i,n;
scanf("%d",&n);
for(i=; i<n; i++) scanf("%d %d",&a[i].x,&a[i].y);
qsort(a,n,sizeof(a[]),cmp);
for(i=; i<n; i++) printf("%d %d\n",a[i].x,a[i].y);
return ;
}

C++中sort函数结构体二级排序:

 #include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
#define N 1005 typedef struct
{
int x,y;
}Point;
Point a[N]; //结构体中按x从小到大排序,x相等的按y从大到小排序
bool cmp(Point a,Point b)
{
if(a.x!=b.x)
return a.x < b.x;
return a.y > b.y;
} int main()
{
int i,n;
scanf("%d",&n);
for(i=; i<n; i++) scanf("%d %d",&a[i].x,&a[i].y);
sort(a,a+n,cmp);
for(i=; i<n; i++) printf("%d %d\n",a[i].x,a[i].y);
return ;
}

C语言qsort函数字符串数组排序:

 #include "stdio.h"
#include "string.h"
#include "stdlib.h"
#define N 1005 typedef struct
{
int data;
char s[N];
}TT; TT str[N];
//对字符串数组按从小到大排序
int cmp(const void *a,const void *b)
{
TT *c = (TT *)a;
TT *d = (TT *)b;
return strcmp(c->s,d->s); //strcmp(s1,s2),若s1>s2,return 1;若s1<s2,return -1;
} int main()
{
int i,n;
scanf("%d",&n);
for(i=; i<n; i++) scanf("%s",str[i].s);
qsort(str,n,sizeof(str[]),cmp);
for(i=; i<n; i++)
puts(str[i].s);
return ;
}

C++中sort函数字符串数组排序:

 #include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
#define N 1005 typedef struct
{
int data;
char s[N];
}TT; TT str[N];
//对字符串数组按从小到大排序
bool cmp(TT a,TT b) //cmp只返回真或假
{
return strcmp(a.s,b.s)==-; //strcmp(s1,s2),若s1>s2,return 1;若s1<s2,return -1;
} int main()
{
int i,n;
scanf("%d",&n);
for(i=; i<n; i++) scanf("%s",str[i].s);
sort(str,str+n,cmp);
for(i=; i<n; i++)
puts(str[i].s);
return ;
}