欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Sort

程序员文章站 2024-03-17 09:01:10
...

Description

Enter an array of length n and rank it in ascending order, ie for any adjacent two numbers a[i], a[i+1], a[i] <= a[i+1].

1 <= n <= 100

1 <= a[i] <= 10^9

Standard Input

The first line is an integer n indicating the length of the number
The next n lines, each line an integer a[i], represent the contents of the array.

Standard Output

Output first behavior array length n
Next n acts as a result of sorting.

Sample

Input Output

4                                                       

4

3

1

2

4

1

2

3

4

Analysis

This is a sorting problem. From the examples, we can see that it is sorted from small to large. In this program, we use the method of selecting sorting.

Code

#include <iostream>

using namespace std;

int main()
{
    int n,number,temp;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++)
    {
        cin>>number;
        a[i]=number;
    }
   
   for(int i=0;i<n-1;i++){
        int min=i;
        for(int j=i+1;j<n;j++){
            if(a[min]>a[j]) min=j;
        }
         temp=a[i];
        a[i]=a[min];
        a[min]=temp;
    }
    cout<<n<<endl;
    for(int i=0;i<n;i++)
    {
        cout<<a[i]<<endl;
    }
    return 0;
}

Result

Sort