快速排序~sort&qsort的用法~以C++

做题的时候经常出现的一种情况就是超时,尤其是排序中的冒泡排序以及快速排序~

这就要用到sort和qsort,在这里呢~就需要知道它们的用法~~

一,sort

1,结构体

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

struct node {
	char a[20];
};
char str[100][1010];
int cmp(char a,char b){
	return strcmp(a,b) < 0;
}

int main (){
	int n;
	scanf("%d", &n);
	for(int i = 0; i < n; ++i)
		scanf("%s",str[i]);
	sort(str, str + n, cmp);
	for(int i = 0; i < n; ++i)
		printf("%s ",str[i]);
	return 0;
}

 

2,整型

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int cmp(int a, int b){
	return a > b;
}

int main ()
{
	int n;
	int a[10];
	scanf("%d",&n);
	for(int i = 0; i < n; ++i)
		scanf("%d", &a[i]);
	sort(a , a + n,cmp);
	for(int i = 0; i < n; ++i)
		printf("%d ", a[i]);
	
	return 0;
}


3,实型

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;


int main ()
{
	int n;
	double a[10];
	scanf("%d",&n);
	for(int i = 0; i < n; ++i)
		scanf("%lf", &a[i]);
	sort(a , a + n);
	for(int i = 0; i < n; ++i)
		printf("%lf ", a[i]);
	return 0;
}


 

4,字符型

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int cmp(char a, char b){
	return a > b;
}

int main (){
	char a[10];
	scanf("%s", a);
	sort(a, a + 3,cmp);
	printf("%s\n", a);
	return 0;
}


二,qsort

1,结构体

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
	int u,w;
	double v;
	char str[1010];
}b[1010];
int cmp(const void *a,const void *b)
{
	if((*(node *)a).u==(*(node *)b).u)
		return (*(node *)b).w-(*(node *)a).w;
	return (*(node *)a).u-(*(node *)b).u;
}
int main()
{
	int n,i;
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		scanf("%d %d",&b[i].u,&b[i].w);
	}
	qsort(b,n,sizeof(b[0]),cmp);
	for(i=0;i<n;i++)
	{
		printf("%d %d\n",b[i].u ,b[i].w);
	}
}


2,整型

#include<stdio.h>
#include<stdlib.h>
int cmp(const void *a,const void *b)
{
	return *(int *)b-*(int *)a;
}
int main()
{
	int n;
	int a[1010];
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	qsort(a,n,sizeof(a[0]),cmp);
	for(i=0;i<n;i++)
	{
		printf("%d ",a[i]);
	}
	printf("\n");
	return 0;
}


3,实型

#include<stdio.h>
#include<stdlib.h>
int cmp(const void *a,const void *b)
{
	if(*(double *)a>*(double *)b)
		return 1;
		return -1;
}
int main()
{
	double a[1010];
	int n,i;
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		scanf("%lf",&a[i]);
	}
	qsort(a,n,sizeof(a[0]),cmp);
	for(i=0;i<n;i++)
	{
		printf("%lf ",a[i]);
	}
	printf("\n");
}


4,字符型

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char str[100][1010];
int cmp(const void *a,const void *b)
{
	return strcmp((char *)b,(char *)a);
}
int main()
{
	int n,i;
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		scanf("%s",str[i]);
	}
	qsort(str,n,sizeof(str[0]),cmp);
	for(i=0;i<n;i++)
	{
		printf("%s\n",str[i]);
	}
	return 0;
}


 

个人觉得sort的比较好用~~结构体的最难记~~如下题~~

EXCEL排序

Problem Description

Excel可以对一组纪录按任意指定列排序。现请你编写程序实现类似功能。

Input

测试输入包含若干测试用例。每个测试用例的第1行包含两个整数 N (<=100000) 和 C,其中 N 是纪录的条数,C 是指定排序的列号。以下有 N
行,每行包含一条学生纪录。每条学生纪录由学号(6位数字,同组测试中没有重复的学号)、姓名(不超过8位且不包含空格的字符串)、成绩(闭区间[0, 100]内的整数)组成,每个项目间用1个空格隔开。当读到 N=0 时,全部输入结束,相应的结果不要输出。

Output

对每个测试用例,首先输出1行“Case i:”,其中 i 是测试用例的编号(从1开始)。随后在 N 行中输出按要求排序后的结果,即:当 C=1 时,按学号递增排序;当 C=2时,按姓名的非递减字典序排序;当 C=3
时,按成绩的非递减排序。当若干学生具有相同姓名或者相同成绩时,则按他们的学号递增排序。

Sample Input

3 1
000007 James 85
000010 Amy 90
000001 Zoe 60
4 2
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 98
4 3
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 90
0 0

Sample Output

Case 1:
000001 Zoe 60
000007 James 85
000010 Amy 90
Case 2:
000010 Amy 90
000002 James 98
000007 James 85
000001 Zoe 60
Case 3:
000001 Zoe 60
000007 James 85
000002 James 90
000010 Amy 90

 

代码如下~

#include<stdio.h>
#include<cstring>
#include<stdlib.h>
#include<algorithm>
using namespace std;
struct str
	{
		int num;
		char name[10];
		int grade;
	}student[100001];
int cmp1(str s1,str s2)
{
	return s1.num<s2.num;
}
int cmp2(str s1,str s2)
{
	if(strcmp(s1.name,s2.name)==0)
		return s1.num<s2.num;
	return strcmp(s1.name,s2.name)<0;
}
int cmp3(str s1,str s2)
{
	if(s1.grade==s2.grade)
		return s1.num<s2.num;
	return s1.grade<s2.grade;
}
int main()
{

	int n,c,i,j=0;
	while(scanf("%d%d",&n,&c)&&n!=0)
	{
		for(i=0;i<n;i++)
			scanf("%d%s%d",&student[i].num,&student[i].name,&student[i].grade);
		if(c==1)
			sort(student,student+n,cmp1);
		if(c==2)
			sort(student,student+n,cmp2);
		if(c==3)
			sort(student,student+n,cmp3);
		printf("Case %d\n",++j);
		for(i=0;i<n;i++)
			printf("%06d %s %d\n",student[i].num,student[i].name,student[i].grade);
	}
	return 0;
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值