三大进阶排序算法

计数排序

找到最大值,然后申请一个最大值+1大小的count数组。

使用count数组来对出现过的数字进行计数统计。此时count数组的索引就是排序好的数字。

遍历count数组,根据count数组的索引和出现的次数生成排序后的结果放入另一个数组中。

复制代码
public static void main(String[] args) {
    int[] a = {3, 9, 7, 2, 5, 8, 1, 4};
    System.out.println(Arrays.toString(a));
    sort(a);
    System.out.println(Arrays.toString(a));
}


private static void sort(int[] a){
    int max = Integer.MIN_VALUE;
    for (int i = 0; i < a.length; i++) {
        if(a[i] > max){
            max = a[i];
        }
    }
    int[] count = new int[max + 1];
    for (int i = 0; i < a.length; i++) {
        count[a[i]]++;
    }
    int i = 0;
    int j = 0;
    while(i < a.length){
        if(count[j] == 0){
            j++;
        }
        int t = count[j];
        while(t > 0 && i < a.length){
            a[i] = j;
            t--;
            i++;
        }
        j++;
    }
}

桶排序

使用动态数组申请一个桶。

遍历原数组,通过除法找到动态数组索引放入数据。

对桶内元素排序,结果放回原数组中。

复制代码
public static void main(String[] args) {
    int[] a = {20, 18, 66, 25, 67, 30};
    System.out.println(Arrays.toString(a));
    sort(a,10);
    System.out.println(Arrays.toString(a));
}


private static void sort(int[] a,int size){
    List<List<Integer>> buckets = new ArrayList<>();
    List<Integer> bucket = null;
    for (int i = 0; i < size; i++) {
        bucket = new ArrayList<>();
        buckets.add(bucket);
    }
    for (int i : a) {
        List<Integer> integerList = buckets.get(i / 10);
        integerList.add(i);
    }
    int k = 0;
    for (List<Integer> integerList : buckets) {
        integerList.sort(Integer::compareTo);
        for (Integer i : integerList) {
            a[k++] = i;
        }
    }
}

基数排序

思想跟桶排序差不多,只不过是从最低位开始排序,通过循环排到最高位。

复制代码
 public static void radixSort(String[] a, int length) {
        ArrayList<String>[] buckets = new ArrayList[128];
        for (int i = 0; i < buckets.length; i++) {
            buckets[i] = new ArrayList<>();
        }
        for (int i = length - 1; i >= 0 ; i--) {
            for (String s : a) {
                buckets[s.charAt(i) - "0"].add(s);
            }
            int k = 0;
            for (ArrayList<String> bucket : buckets) {
                for (String s : bucket) {
                    a[k++] = s;
                }
                bucket.clear();
            }
        }
    }


    public static void main(String[] args) {
        String[] phoneNumbers = new String[10];
        phoneNumbers[0] = "138";
        phoneNumbers[1] = "139";
        phoneNumbers[2] = "136";
        phoneNumbers[3] = "137";
        phoneNumbers[4] = "135";
        phoneNumbers[5] = "134";
        phoneNumbers[6] = "150";
        phoneNumbers[7] = "151";
        phoneNumbers[8] = "152";
        phoneNumbers[9] = "157";
        RadixSort.radixSort(phoneNumbers, 3);
        for (String phoneNumber : phoneNumbers) {
            System.out.println(phoneNumber);
        }
    }
相关推荐
计算机毕业设计木哥13 分钟前
计算机毕设选题推荐:基于Java+SpringBoot物品租赁管理系统【源码+文档+调试】
java·vue.js·spring boot·mysql·spark·毕业设计·课程设计
青衫客3613 分钟前
Spring异步编程- 浅谈 Reactor 核心操作符
java·spring·响应式编程
Seven9713 分钟前
剑指offer-30、连续⼦数组的最⼤和
java
BenChuat17 分钟前
Java常见排序算法实现
java·算法·排序算法
熙客18 分钟前
SpringCloud概述
java·spring cloud·微服务
a5876933 分钟前
Elasticsearch核心概念与Java实战:从入门到精通
java·es
元亓亓亓38 分钟前
LeetCode热题100--105. 从前序与中序遍历序列构造二叉树--中等
算法·leetcode·职场和发展
纪元A梦1 小时前
贪心算法在SDN流表优化中的应用
算法·贪心算法
JCBP_1 小时前
QT(4)
开发语言·汇编·c++·qt·算法
码熔burning1 小时前
JVM 垃圾收集算法详解!
jvm·算法