Sorting machine test guidelines Example 2.1

Title Description

    The number n of inputs and outputs sort.

Enter a description:

    Comprising a first line of input integer n (1 <= n <= 100). 
    The next line comprises n integers.

Output Description:

    May be multiple sets of test data for each set of data, the n integers sorted output, has a space behind each number. 
    Each set of test data per line.
Example 1

Entry

4
1 4 3 2

Export

1 2 3 4 
2.1 bubble sort codes
#include<stdio.h>
int main()
{
    int n;
    int buf[100];
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d",&buf[i]);
        }
        for(int i=0;i<n;i++)         //冒泡排序
        {
            for(int j=0;j<n-1-i;j++)
            {
                if(buf[j]>buf[j+1])
                {
                    int temp=buf[j];
                    buf[j]=buf[j+1];
                    buf[j+1]=temp;
                }
            }
        }
        for(int i=0;i<n;i++)
        {
            printf("%d ",buf[i]);
        }
        printf("\n");
    }
    return 0;
}

  2.2 Use Code C ++ library function quicksort

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int n;
    int buf[100];
    while(cin>>n&&n)
    {
        for(int i=0;i<n;i++)
        {
            cin>>buf[i];
        }
        sort(buf,buf+n);    //使用快速排序
        for(int i=0;i<n;i++)
        {
            cout<<buf[i]<<" ";
        }
        cout<<endl;
    }
    return 0;
}

  2.3 defines the code collation in descending order to achieve

#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(int x,int y)
{
    return x>y;
}
int main()
{
    int n;
    int buf[100];
    while(cin>>n&&n)
    {
        for(int i=0;i<n;i++)
        {
            cin>>buf[i];
        }
        sort(buf,buf+n,cmp);
        for(int i=0;i<n;i++)
        {
            cout<<buf[i]<<" ";
        }
        cout<<endl;
    }
    return 0;
}

  

 

Guess you like

Origin www.cnblogs.com/qing123tian/p/11109221.html
Recommended