美文网首页
计数排序

计数排序

作者: ZuYuan | 来源:发表于2019-07-18 21:11 被阅读0次
  • 归属:时空权衡思想中的输入增强技术
  • 简介:该算法比较简单。针对待排序列表中的每一个元素,算出列表中小于该元素的元素个数,并把结果记录在一张表中。
  • 算法:
    public static int[] comparisonCountingSort(int[] nums) {
        //判断这个的nums的范围应该是0~11
        //Java int 数组元素默认值是0
        int counts[] = new int[12];
        for (int i : nums) {
            counts[i] = counts[i] + 1;
        }
        return counts;
    }

    public static void main(String[] args) {
        int[] ns = {1, 4, 3 ,6, 2, 11, 8, 4, 9, 8};
        int[] counts = comparisonCountingSort(ns);
        System.out.println();
        for (int i = 0; i < counts.length; i++) {
            if (counts[i] > 0) {
                for (int j = 0; j < counts[i]; j++) {
                    System.out.print(i + " ");
                }
            }
        }
    }

相关文章

网友评论

      本文标题:计数排序

      本文链接:https://www.haomeiwen.com/subject/iobrlctx.html