zoukankan      html  css  js  c++  java
  • 计数排序and基数排序

    1 计数排序,稳定    复杂度o(k + n)

        public static int[] countingSort(int[] nums) {
            int n = nums.length;
            int k = 0;
            for (int i = 0; i < n; i++) {
                k = Math.max(k, nums[i]);
            }
            int[] count = new int[k + 1];
            int[] res = new int[n];
            for (int i = 0; i < n; i++) {
                count[nums[i]]++;
            }
            for (int i = 1; i <= k; i++) {
                count[i] += count[i - 1];
            }
            for (int i = n - 1; i >= 0; i--) {
                res[--count[nums[i]]] = nums[i];
            }
            return res;
        }
    View Code

    2 基数排序 需要稳定排序  有n个d位数,每一位有k个取值,复杂度为d(n + k)

        public static int[] radixSort(int[] nums) {
            int max = 0;
            int n = nums.length;
            for (int i = 0; i < nums.length; i++) {
                max = Math.max(max, nums[i]);
            }
            int exp = 1;
            int[] cur = new int[n];
            while (max / exp > 0) {
                int[] count = new int[10];            
                for (int i = 0; i < n; i++) {
                    count[(nums[i] / exp) % 10]++;
                }
                for (int i = 1; i < 10; i++) {
                    count[i] += count[i - 1];
                }
                for (int i = n - 1; i >= 0; i--) {
                    cur[--count[(nums[i] / exp) % 10]] = nums[i];
                }
                for (int i = 0; i < n; i++) {
                    nums[i] = cur[i];
                }
                exp *= 10;
            }
            return nums;
        }
    View Code

    3 桶排序

  • 相关阅读:
    06_springboot热部署
    05_springboot多配置文件
    04_springBoot端口和上下文路径
    03_springboot错误处理.md
    02_springboot部署-jar和-war的方式
    01_idea创建springboot
    Vue学习笔记
    Java使用Swing实现五子棋。
    我的开源项目
    Java常用库和工具类
  • 原文地址:https://www.cnblogs.com/whesuanfa/p/7534892.html
Copyright © 2011-2022 走看看