HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavaMinor

Radix sorts in Java

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
radixjavasorts

Problem

I was curious, how in-place radix sort compares to the variant which uses an auxiliary array in order to speed up the sorting. I implemented both and tested it against java.util.Arrays.sort(int[]).

Seed: 1439451337582
Radixsort.InPlace.sort in 12074 milliseconds.
Radixsort.sort in 6937 milliseconds.
Arrays.sort in 16316 milliseconds.
Arrays identical: true


What do you think?

Radixsort.java:

`package net.coderodde.util;

import java.util.Arrays;

/**
* This class implements a radix sort using an auxiliary array in order to speed
* up sorting.
*
* @author Rodion "rodde" Efremov
* @version 1.6
*/
public class Radixsort {

/**
* The byte index of the most significant byte in each 32-bit integer.
*/
private static final int MOST_SIGNIFICANT_BYTE_INDEX = 3;

/**
* The mask for manipulating the sign bit.
*/
private static final int SIGN_BIT_MASK = 0x8000_0000;

/**
* The amount of bits per byte.
*/
private static final int BITS_PER_BYTE = 8;

/**
* The mask for extracting the bucket index.
*/
private static final int EXTRACT_BYTE_MASK = 0xff;

/**
* The amount of buckets considered for sorting.
*/
private static final int BUCKET_AMOUNT = 256;

/**
* If the range length is less than this constant use
* {@link java.util.Arrays.sort} and exit.
*/
private static final int QUICKSORT_THRESHOLD = 128;

/**
* This inner static class provides an in-place implementation of the radix
* sort.
*/
public static final class InPlace {

/**
* Sorts the range {@code array[fromIndex], array[fromIndex + 1], ...,
* array[toIndex - 2], array[toIndex - 1]}.
*
* @param array the array holding the target range.
* @param fromIndex the starting, inclusive index of the range.
* @param toIndex the ending, exclusive index of the range.
*/
public static void sort(int[]

Solution

Not much to say about this lovely well-documented code, it's very well written. Just some notes:

private static int getBucketIndex(int element, int byteIndex) {
    return ((byteIndex == MOST_SIGNIFICANT_BYTE_INDEX ? 
             element ^ SIGN_BIT_MASK :
             element) >>> (byteIndex * BITS_PER_BYTE)) & EXTRACT_BYTE_MASK;
}


The return statement looks ugly and hard to read. Turn it into an if statement:

private static int getBucketIndex(int element, int byteIndex) {
    int result = element;
    if (byteIndex == MOST_SIGNIFICANT_BYTE_INDEX) {
        result ^= SIGN_BIT_MASK;
    }
    return (result >>> (byteIndex * BITS_PER_BYTE)) & EXTRACT_BYTE_MASK;
}

Code Snippets

private static int getBucketIndex(int element, int byteIndex) {
    return ((byteIndex == MOST_SIGNIFICANT_BYTE_INDEX ? 
             element ^ SIGN_BIT_MASK :
             element) >>> (byteIndex * BITS_PER_BYTE)) & EXTRACT_BYTE_MASK;
}
private static int getBucketIndex(int element, int byteIndex) {
    int result = element;
    if (byteIndex == MOST_SIGNIFICANT_BYTE_INDEX) {
        result ^= SIGN_BIT_MASK;
    }
    return (result >>> (byteIndex * BITS_PER_BYTE)) & EXTRACT_BYTE_MASK;
}

Context

StackExchange Code Review Q#100791, answer score: 2

Revisions (0)

No revisions yet.