Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.0 kB
2
Indexable
Never
    public static void lsdRadixSort(int[] arr) {
        // WRITE YOUR CODE HERE (DO NOT MODIFY METHOD HEADER)!
        LinkedList<Integer> buckets[] = new LinkedList[19];
        for (int i = 0; i < 19; i++) buckets[i] = new LinkedList<>();;
        boolean finished = false;
        long div = 1;
        while (!finished) {
            finished = true;
            for (int num: arr) {
                if (div == 0) {
                    div++;
                }
                long nom = Math.abs((long)num) / div;
                if (nom > 0) finished = false;
                int digit = (int)(nom % 10);
                if (num < 0) digit *= -1;
                digit += 9;
                buckets[digit].add(num);
            }
            div *= 10;
            int writeIndex = 0;
            for (LinkedList<Integer> bucket: buckets) {
                while (!bucket.isEmpty()) {
                    arr[writeIndex] = bucket.remove();
                    writeIndex++;
                }
            }
        }
    }
}
Leave a Comment