美文网首页
复习基数排序

复习基数排序

作者: Ethan_Walker | 来源:发表于2019-02-21 00:31 被阅读0次

public class LSD {

    class Node {
        int val;
        Node next;

        public Node(int val) {
            this.val = val;
        }

        @Override
        public String toString() {
            return val + "";
        }
    }

    // 桶内结构
    class LinkedList {
        Node head;
        Node tail;
    }

    public void LSDSort(int[] nums) {
        if (nums == null || nums.length <= 1) return;

        // 遍历得到最大值,同时将数组初始化为一个链表
        Node head = new Node(nums[0]);
        Node prev = head, cur = null;
        int maxVal = nums[0];
        for (int i = 1; i < nums.length; i++) {
            cur = new Node(nums[i]);
            prev.next = cur;
            prev = cur;
            if (nums[i] > maxVal) {
                maxVal = nums[i];
            }
        }
        prev.next = null;

        // 初始桶
        LinkedList[] bucket = new LinkedList[10];
        for (int i = 0; i < 10; i++) {
            bucket[i] = new LinkedList();
        }


        int posDiv = 1, remain = 0; //  posDiv = 1 表示求个位 , 10 十位, 100..
        cur = head;
        Node next, tail;
        while (maxVal / posDiv != 0) {
            cur = head;
            // 分配
            while (cur != null) {
                next = cur.next;
                cur.next = null;
                remain = cur.val / posDiv % 10;
                if (bucket[remain].head == null) {
                    bucket[remain].head = cur;
                    bucket[remain].tail = cur;
                } else {
                    // nums[i] 添加到 链尾
                    bucket[remain].tail.next = cur;
                    bucket[remain].tail = cur;
                }
                cur = next;
            }

            // 收集
            head = null;
            tail = null;
            for (int i = 0; i < 10; i++) {
                if (bucket[i].head != null) {
                    if (head == null) {
                        head = bucket[i].head;
                        tail = bucket[i].tail;
                    } else {
                        tail.next = bucket[i].head;
                        tail = bucket[i].tail;
                    }
                }
                bucket[i].head = bucket[i].tail = null; // 清空
            }
            posDiv *= 10;
        }

        int i = 0;
        while (head != null) {
            nums[i++] = head.val;
            head = head.next;
        }
    }


    @Test
    public void test() {

        for (int k = 0; k < 10000; k++) {
            Random random = new Random();
            int length = random.nextInt(2000);
            int[] a = new int[length];

            for (int i = 0; i < length; i++) {
                a[i] = random.nextInt(2000);
            }
            LSDSort(a);

            boolean judge = SortJudge.judge(a);
            if (!judge) {
                System.out.println(judge);
            }
        }
    }

}

相关文章

  • 复习基数排序

  • 基数排序(c++) to be continued

    [TOC] 参考 基数排序算法的实现与优化 clickhouse 实现的基数排序源码 基数排序的性能优化 Radi...

  • 数组-基数排序

    采用基数排序方式对数组进行排序 基数排序百科:基数排序排序(Distribution Sort),属于分配式排序,...

  • day12-基数排序

    基数排序 基数排序(radix sort)属于“分配式排序”(distribution sort),又称“桶子法”...

  • swift经典算法-基数排序

    基数排序算法 基数排序(radix sort)属于“分配式排序”(distribution sort),又称“桶子...

  • php-计数排序、基数排序、桶排序

    计数排序、基数排序、桶排序 时间复杂度 O(n) 计数排序 基数排序 桶排序

  • 数据结构之基数排序

    1.基数排序(桶排序)介绍 基数排序(radix sort)属于“分配式排序”(distribution sort...

  • 基数排序(Radix Sort)

    基本思想: 基数排序是一种有意思的排序,在看过其它比较排序后,基数排序真的很有意思。 基数排序(Radix Sor...

  • 5分钟了解基数排序

    5分钟了解基数排序 前言 基数排序无需进行比较和交换,而是利用分配和收集两种基本操作实现排序。基数排序分为两种:第...

  • 算法面经--基数排序

    基数排序 一、算法思路 1.简单介绍 1)基数排序法是属于稳定性的排序,基数排序法的是效率高的稳定性排序法 基数排...

网友评论

      本文标题:复习基数排序

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