zoukankan      html  css  js  c++  java
  • 对公司所有员工的年龄排序

    题目: 对公司所有员工的年龄排序,总共几万人,要求实现一个排序算法,要求时间效率为O(n),可以使用辅助内存,只允许使用常量大小的辅助空间

    分析:员工的年龄假设为0~10,故辅助数组为timeOfAge[10],保存每个年龄出现的次数

    public class SortAges {
        public static void sortAge(int age[]){
            final int oldAge=10;
            int timesOfAge[]=new int[oldAge];
            int len=age.length;
            //timeofage保存重复出现的次数
            for (int i = 0; i <len ; i++) {
                timesOfAge[age[i]]++;
            }
            int index=0;
            for (int i = 0; i <oldAge ; i++) {
                for (int j = 0; j <timesOfAge[i] ; j++) {
                    age[index]=i;
                    index++;
                }
            }
        }
    
        public static void main(String[] args) {
            int[] ages=new int[]{2,4,3,2,7,5,3};
            sortAge(ages);
            for (int i = 0; i <ages.length ; i++) {
                System.out.println(ages[i]);
            }
        }
    }

      

  • 相关阅读:
    P1726 上白泽慧音
    P1993 小k的农场
    P1983 车站分级
    P1525 关押罪犯【二分+二分图】
    P1268 树的重量【构造】
    P1113 杂务
    F.Three pahs on a tree
    P1522 牛的旅行
    两个约束下的dp问题
    dp 最大正方形
  • 原文地址:https://www.cnblogs.com/yeleia/p/9999974.html
Copyright © 2011-2022 走看看