三大进阶排序算法

计数排序

找到最大值,然后申请一个最大值+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);
        }
    }
  • 25
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码已经练习两年半

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值