基础练习——数列排序

资源限制
时间限制:1.0s   内存限制:512.0MB
问题描述
  给定一个长度为n的数列,将这个数列按从小到大的顺序排列。1<=n<=200
输入格式
  第一行为一个整数n。
  第二行包含n个整数,为待排序的数,每个整数的绝对值小于10000。
输出格式
  输出一行,按从小到大的顺序输出排序后的数列。
样例输入
5
8 3 6 4 9
样例输出
3 4 6 8 9
#include<iostream>
using namespace std;

void selectionsort(int num[],int n)
{
    int j;
    for(int i=1;i<=n;i++){
        int k=i;
        for(j=i;j<=n;j++){
            if(num[j]<num[k])
            k=j;
        }
    int temp=num[i];
    num[i]=num[k];
    num[k]=temp;
    }
    
}
int main()
{
    int n;
    while(cin >> n)
    {
        int a[1001];
        for(int i=1;i<=n;i++)
        cin >> a[i];
        selectionsort(a,n);
        for(int j=1;j<=n-1;j++)
        cout << a[j] << ' ';
        cout << a[n] << endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/wlyperfect/p/12488669.html