• 算法练习1桶排序java版


    今天复习了桶排序。

    例如现在有满分为10分的试卷,学生得分分别为2,8,5,3,5,7,现在要给这些分数按照从大到小输出,使用桶排序的思想:有11个桶,每个桶有一个编号,编号从0-10,每出现一个分数,则在相应编号的桶里面插入一个小旗子,最后按照旗子的数目分别输出桶的编号并对编号进行由大到小的排序。我用java代码实现的桶排序为:

     1 public class PaiXu {
     2 
     3     public static void main(String[] args) {
     5         /*
     6          * 20170120桶排序
     7          */
     8         PaiXu px = new PaiXu();
     9         int[] score = {5,3,5,2,8,0,7,10,4};
    10         int[] sf = px.f(score);
    11         for(int k=10;k>=0;k--){
    12             if(sf[k]!=0){
    13                 for(int a=1;a<=sf[k];a++){
    14                     System.out.print(k+"    ");
    15                 }
    16             }
    17         }
    18     }
    19     /*
    20      *  @param score 分数(0-10分)
    21      *  @return 返回计数数组
    22      *  说明:使用数组来记录0-10出现的次数,所以定义数组长度为11,数组中每一个数初始赋值为0
    23      */
    24     public int[] f(int[] score){
    25         int[] outputsc = new int[11];
    26         for(int i=0;i<=10;i++){
    27             outputsc[i]=0;
    28         }
    29         for(int j=0;j<score.length;j++){
    30             outputsc[score[j]]++;
    31         }
    32         return outputsc;
    33     }
    34     
    35 }

    执行结果为:

    10    8    7    5    5    4    3    2    0    
  • 相关阅读:
    之所以菜鸟依旧
    单点登陆
    让entityframework.extend库同时支持mysql,sqlsever
    背包算法
    JS中实现继承
    Altium Designer 生成 Mach3 G代码的程序
    test博客嵌入pbi
    testPBI报表
    html中隐藏title属性方法
    Spring mvc 中有关 Shiro 1.2.3 配置问题
  • 原文地址:https://www.cnblogs.com/paopaoquan/p/6323191.html
Copyright © 2020-2023  润新知