zcmu 1030 Age Sort(含内置函数sort排序,冒泡排序,选择排序,归并排序,快速排序)

题目描述

You are given the ages (in years) of allpeople of a country with at least 1 year of age. You know that no individual in that country lives for 100 or more years. Now, you are given a very simple task of sorting all the ages in ascending order.

你得到一个国家所有年龄至少为1岁的人的年龄(以年为单位)。你知道这个国家的任何一个人都活不到100岁或更老。现在,你得到了一个非常简单的任务,那就是将所有年龄按升序排列。

输入

There are multiple test cases in the input file. Each case starts with an integer (0<n<=1000000), the total number of people. In the next line, there are integers indicating the ages. Input is terminated with a case where = 0. This case should not be processed.

输入文件中包含多个测试用例。每个用例以整数n(0<n<=1000000)开始,表示总人数。下一行有n个整数表示年龄。输入以n=0的用例结束。此用例不应被处理。

输出

For each case, print a line with space separated integers. These integers are the ages of that country sorted in ascending order.

对于每个案例,输出一行,用n个空格分隔整数。这些整数是按升序排列的该国的年龄。

样例输入

5 3 4 2 1 5 5 2 3 2 3 1 0

样例输出

1 2 3 4 5 1 2 2 3 3

以下是排序的五种方法

 1. 使用sort函数排序

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    while(scanf("%d",&n)&&n!=0){
        int a[n];
        for(int i=0;i<n;i++){
            scanf("%d",&a[i]);
        }
        sort(a,a+n);                                //sort(初位置,末位置,顺序)
        cout<<a[0];
        for(int i=1;i<n;i++){
            printf(" %d",a[i]);
        }
        printf("\n");
        }
    return 0;
}

2. 使用冒泡排序

#include<bits/stdc++.h>
using namespace std;
void bubble(int* list,int n){                       //冒泡排序flag版本
    int flag;
    for(int i=0;i<n;i++){
        flag=1;
        for(int j=n-1;j>i;j--){
            if(list[j]<list[j-1]){
                int num=list[j];
                list[j]=list[j-1];
                list[j-1]=num;
                flag=0;
            }
        }
        if(flag){
            break;
        }
    }
}
int main(){
    int n;
    while(scanf("%d",&n)&&n!=0){
        int a[n];
        for(int i=0;i<n;i++){
            scanf("%d",&a[i]);
        }
        bubble(a,n);                                //bubble(需要排序数组,总长度)
        cout<<a[0];
        for(int i=1;i<n;i++){
            printf(" %d",a[i]);
        }
        printf("\n");
        }
    return 0;
}

3. 使用选择排序

#include<bits/stdc++.h>
using namespace std;
void Selection(int* list,int n){                    //选择排序flag版本
    int flag;
    for(int i=0;i<n;i++){
        flag=1;
        for(int j=n-1;j>i;j--){
            if(list[j]<list[i]){
                int num=list[j];
                list[j]=list[i];
                list[i]=num;
                flag=0;
            }
        }
        if(flag){
            break;
        }
    }
}
int main(){
    int n;
    while(scanf("%d",&n)&&n!=0){
        int a[n];
        for(int i=0;i<n;i++){
            scanf("%d",&a[i]);
        }
        Selection(a,n);                             //Selection(需要排序数组,总长度)
        cout<<a[0];
        for(int i=1;i<n;i++){
            printf(" %d",a[i]);
        }
        printf("\n");
        }
    return 0;
}

4. 使用归并排序

#include<bits/stdc++.h>
using namespace std;
void mergesort(int* list,int low,int mid,int high,int* tmp){
    int i=0;                                        //用于遍历tmp数组的指针
    int j=low,k=mid+1;                              //左边序列和右边序列起始索引
    while(j<=mid&&k<=high){                         //比较左右两个有序数组的元素,并按大小依次放入tmp数组中
        if(list[j]<list[k]){                        //左半区第一个元素更小,先放左半区第一个元素
            tmp[i++]=list[j++];             
        }
        else{                                       //右半区第一个元素更小,先放右半区第一个元素
            tmp[i++]=list[k++];
        }
    }
    while(j<=mid){                                  //若左边序列还有剩余,则将其全部拷贝进tmp[]中
        tmp[i++]=list[j++];
    }
    while(k<=high){                                 //若右边序列还有剩余,则将其全部拷贝进tmp[]中
        tmp[i++]=list[k++];
    }
    for(int t=0;t<i;t++){                           //将排好序的tmp数组复制到原数组list中
        list[low+t]=tmp[t];
    }
}
void merge(int* list,int low,int high,int* tmp){    //归并排序
    if(low<high){
        int mid=(low+high)/2;                       //mid是low和high的平均数
        merge(list,low,mid,tmp);
        for(int i=low;i<=mid;i++){                  //测试输出,提交时需要删除
            printf(" %d",list[i]);
        }
        printf("\n");
        merge(list,mid+1,high,tmp);
        for(int i=mid+1;i<=high;i++){               //测试输出,提交时需要删除
            printf(" %d",list[i]);
        }
        printf("\n");
        mergesort(list,low,mid,high,tmp);
    }
}
int main(){
    int n;
    while(scanf("%d",&n)&&n!=0){
        int a[n];
        int ans[n];
        for(int i=0;i<n;i++){
            scanf("%d",&a[i]);
        }
        merge(a,0,n-1,ans);                         //merge(需要排序数组,最小地址,最大地址,保存排序数组)
        cout<<a[0];
        for(int i=1;i<n;i++){
            printf(" %d",a[i]);
        }
        printf("\n");
        }
    return 0;
}

5. 使用快速排序

#include<bits/stdc++.h>
using namespace std;
void quick(int* list,int low,int high){
    int i=low;                                      //用于从左边遍历list数组的指针
    int j=high;                                     //用于从右边遍历list数组的指针
    if(low>high){
        return;
    }
    while(i<j){
        while(list[low]<=list[j]&&i<j){             //右半区,从右数第一个小于list[low]的元素
            j--;
        }
        while(list[low]>=list[i]&&i<j){             //左半区,从左数第一个大于list[low]的元素
            i++;
        }
        int num=list[j];                            //交换左右数值,使得左边都小于list[low],右边都大于list[low]
        list[j]=list[i];
        list[i]=num;
    }
    int num=list[j];                                //交换首元素和i,j碰撞点的元素
    list[j]=list[low];
    list[low]=num;
    for(int i=low;i<=high;i++){                     //测试输出,提交时需要删除
        printf(" %d",list[i]);
    }
    printf("\n");
    quick(list,low,j-1);
    quick(list,j+1,high);
}
int main(){
    int n;
    while(scanf("%d",&n)&&n!=0){
        int a[n];
        for(int i=0;i<n;i++){
            scanf("%d",&a[i]);
        }
        quick(a,0,n-1);                             //quick(需要排序数组,最小地址,最大地址)
        cout<<a[0];
        for(int i=1;i<n;i++){
            printf(" %d",a[i]);
        }
        printf("\n");
        }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值