You are given the ages (in years) of all people of a country with at least 1 year of age. You know thatno individual in that country lives for 100 or more years. Now, you are given a very simple task ofsorting all the ages in ascending order.
Input
There are multiple test cases in the input file. Each case starts with an integer n (0 < n ≤ 2000000), thetotal number of people. In the next line, there are n integers indicating the ages. Input is terminatedwith a case where n = 0. This case should not be processed.
Output
For each case, print a line with n space separated integers. These integers are the ages of that countrysorted in ascending order.
Warning: Input Data is pretty big (∼ 25 MB) so use faster IO.
Sample Input
5
3 4 2 1 5
5
2 3 2 3 1
0
Sample Output
1 2 3 4 5
1 2 2 3 3
实现代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define Num 2000020
int age[Num];
int main()
{
int n,test;
while(scanf("%d",&n)==1&&n)
{
for(int i=0;i<n;i++)
scanf("%d",&age[i]);
sort(age,age+n);
for(int i=0;i<n-1;i++)
{
printf("%d ",age[i]);
test=i;
}
printf("%d\n",age[test+1]);
}
return 0;
}
排序:
普通三个数排序
1.利用for循环
for(i=0;i<3;i++)
if (a>b)
{ tmp=a;a=b; b=tmp; }
if (b>c)
{ tmp=b;b=c; c=tmp; }
2.利用三个if语句
if(a>b)
(temp=a;a=b;b=temp)
if(a>c)
(temp=a;a=c;c=temp)
if(b>c)
(temp=b;b=c;c=temp)
3.利用选择排序法
void SetList(int array[],int cnout)
{
int i,j,k,temp;
for(i=0;i=cnout-1;i++)
{
k=i;
for(j=1;j<cnout;j++)
{ if(array[k]<array[j])
k=j;
temp=array[k];array[k]=array[j];array[j]=temp;
}
}
4,利用c++ STL sort()函数
sort(vector.begin(),vector.end())
5、利用c++ STL sort() 反序排序
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
bool Comp(const int &a,const int &b)
{
if(a!=b)
return a>b;
else
return a>b;
}
int main()
{
vector<int> num_list;
for(int i=0;i<10;i++)
num_list.push_back(i);
for(int i=0;i<10;i++)
cout<<num_list[i]<<" ";
cout<<endl;
sort(num_list.begin(),num_list.end(),Comp);
for(int i=0;i<10;i++)
cout<<num_list[i]<<" " ;
cout<<endl;
return 0;
}
![反序实现](http://img.blog.csdn.net/20150717093627586)