现在的位置: 首页 > 综合 > 正文

Sort 排序的几种用法

2013年06月12日 ⁄ 综合 ⁄ 共 3493字 ⁄ 字号 评论关闭

 

PS: 之前给新生们写的。在这里也发一下吧。

 

====================================================================================

 

【整数数组】直接从小到大排序 

 

#include<algorithm>
#include<cstdio>

using namespace std;

//输入: 
//    先输入数组长度 n
//    然后再输入n个整数
//输出
//    从小到大顺序输出数组 

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

 

======================================================================================

 

【整数数组】从小到大排序

 

#include<algorithm>
#include<cstdio>

using namespace std;

//输入: 
//    先输入数组长度 n
//    然后再输入n个整数
//输出
//    从小到大顺序输出数组 

int cmp(int x,int y){
	return x<y;
}

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

 

==================================================================================

 

【整数数组】从大到小排序

 

#include<algorithm>
#include<cstdio>

using namespace std;

//输入: 
//    先输入数组长度 n
//    然后再输入n个整数
//输出
//    从大到小顺序输出数组 

int cmp(int x,int y){
	return x>y;
}

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

======================================================================================

【结构体数组】简单排序 

 

#include<algorithm>
#include<cstdio>

using namespace std;

//输入: 
//    先输入结构体数组长度 n
//    然后再输入n个整数,表示结构体的其中的一个属性q 
//输出
//    根据结构体数组的属性q的从大到小顺序,输出结构体数组 
struct S{
	int q;
	int b;
}a[200];

int cmp(S x,S y){
	return x.q>y.q;
}

int main(){
	int i,n;
	scanf("%d",&n);
	for(i=0;i<n;i++){
		scanf("%d",&a[i].q);
		a[i].b=i;
	}
	sort(a,a+n,cmp);
	for(i=0;i<n;i++)
		printf("%d 原位置:%d\n",a[i].q,a[i].b);
	puts("");
	return 0;
}

 

==============================================================================

 

【结构体数组】双属性排序

 

#include<algorithm>
#include<cstdio>

using namespace std;

//输入: 
//    先输入结构体数组长度 n
//    然后接下去n行,每行输入两个整数,表示结构体的其中的两个属性p和q 
//输出
//    根据p属性的大小进行从大到小排序,在p相等的情况下,则根据q的大小进行从大到小排序 

/*
样例输入:
5
3 5
4 7
6 3
4 9
3 2 

*/ 

struct S{
	int q;
	int p;
}a[200];

int cmp(S x,S y){
	if(x.p==y.p)
		return x.q>y.q;
	return x.p>y.p;
}

int main(){
	int i,n;
	scanf("%d",&n);
	for(i=0;i<n;i++)
		scanf("%d%d",&a[i].p,&a[i].q);
	sort(a,a+n,cmp);
	puts("排序后:"); 
	for(i=0;i<n;i++)
		printf("%d %d\n",a[i].p,a[i].q);
	puts("");
	return 0;
}

 

==============================================================================

 

【结构体数组】双属性排序2    //   所有的结构体排序都可以把以上的cmp函数,写成结构体运算符重载operator<

 

#include<algorithm>
#include<cstdio>

using namespace std;

//输入: 
//    先输入结构体数组长度 n
//    然后接下去n行,每行输入两个整数,表示结构体的其中的两个属性p和q 
//输出
//    根据p属性的大小进行从大到小排序,在p相等的情况下,则根据q的大小进行从大到小排序 

/*
样例输入:
5
3 5
4 7
6 3
4 9
3 2 

*/ 

struct S{
	int q;
	int p;
	bool friend operator <(S a,S b){
		if(a.p==b.p) return a.q>b.q;
		return a.p>b.p;
	}
}a[200];

int main(){
	int i,n;
	scanf("%d",&n);
	for(i=0;i<n;i++)
		scanf("%d%d",&a[i].p,&a[i].q);
	sort(a,a+n);
	puts("排序后:"); 
	for(i=0;i<n;i++)
		printf("%d %d\n",a[i].p,a[i].q);
	puts("");
	return 0;
}

=================================================================================================

 

【字符串数组】排序(char字符串)

 

#include<algorithm>
#include<cstdio>
#include<cstring>

using namespace std;

//输入: 
//    先输入字符串数组长度 n
//    然后再输入n个字符串 
//输出
//    从小到大顺序输出字符串数组 

char as[200][50];

int cmp(int x,int y){
	return strcmp(as[x],as[y])==-1;
} 

int main(){
	int i,n,a[200];
	
	scanf("%d",&n);
	for(i=0;i<n;i++){  // 注意字符串数组as[] 顺序并没有改变,改变的是存储 字符串数组as[] 映射 的数组a[] 
		scanf("%s",as[i]);
		a[i]=i;
	}
	sort(a,a+n,cmp);
	for(i=0;i<n;i++)
		printf("%s\n",as[a[i]]);
	return 0;
}

 

=================================================================================================

 

【字符串数组】排序(string 字符串)

#include<algorithm>
#include<iostream>

using namespace std;

//输入: 
//    先输入字符串数组长度 n
//    然后再输入n个字符串 
//输出
//    从小到大顺序输出字符串数组 

string as[100];

int main(){
	int i,n,a[200];
	scanf("%d",&n);
	for(i=0;i<n;i++)
		cin>>as[i];
	sort(as,as+n);
	cout<<"排序后:"<<endl;
	for(i=0;i<n;i++)
		cout<<as[i]<<endl;
	return 0;
}

 

 

 

 

抱歉!评论已关闭.