Skip to content

Instantly share code, notes, and snippets.

@amaembo
Created October 22, 2015 05:55
Show Gist options
  • Save amaembo/8e8455ff34012ea97508 to your computer and use it in GitHub Desktop.
Save amaembo/8e8455ff34012ea97508 to your computer and use it in GitHub Desktop.
Fast radix sort implementation for Java
/*
Refactored by Tagir Valeev
Original code taken from
https://raw.githubusercontent.com/gorset/radix/master/Radix.java
Below is the original copyright notice
--------------
Copyright 2011 Erik Gorset. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list
of conditions and the following disclaimer in the documentation and/or other materials
provided with the distribution.
THIS SOFTWARE IS PROVIDED BY Erik Gorset ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Erik Gorset OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those of the
authors and should not be interpreted as representing official policies, either expressed
or implied, of Erik Gorset.
*/
import java.util.Arrays;
import java.util.Random;
public class RadixSort {
private static final int INSERTION_SORT_THRESHOLD = 64;
public static void sort3(int[] array, int offset, int end, int[] pointer) {
Arrays.fill(pointer, 0, 0x100, 0);
for (int x = offset; x < end; ++x) {
++pointer[array[x] & 0xFF];
}
pointer[0] += offset;
pointer[0x400] = offset;
for (int x = 1; x < 0x100; ++x) {
int off = pointer[x - 1];
pointer[x + 0x400] = off;
pointer[x] += off;
}
for (int x = 0; x < 0x100; ++x) {
int off = pointer[x + 0x400];
while (off != pointer[x]) {
int value = array[off];
int y = value & 0xff;
if(x == y) {
off++;
} else {
while (x != y) {
int temp = array[pointer[y + 0x400]];
array[pointer[y + 0x400]++] = value;
value = temp;
y = value & 0xff;
}
array[off++] = value;
}
}
pointer[x + 0x400] = off;
}
}
public static void sort2(int[] array, int offset, int end, int[] pointer) {
Arrays.fill(pointer, 0, 0x100, 0);
for (int x = offset; x < end; ++x) {
++pointer[(array[x] >> 8) & 0xFF];
}
pointer[0] += offset;
pointer[0x300] = offset;
for (int x = 1; x < 0x100; ++x) {
int off = pointer[x - 1];
pointer[x + 0x300] = off;
pointer[x] += off;
}
for (int x = 0; x < 0x100; ++x) {
int off = pointer[x + 0x300];
while (off != pointer[x]) {
int value = array[off];
int y = (value >> 8) & 0xff;
if(x == y) {
off++;
} else {
while (x != y) {
int temp = array[pointer[y + 0x300]];
array[pointer[y + 0x300]++] = value;
value = temp;
y = (value >> 8) & 0xff;
}
array[off++] = value;
}
}
pointer[x + 0x300] = off;
}
for (int x = 0; x < 0x100; ++x) {
int off = pointer[x + 0x300];
int size = x > 0 ? off - pointer[x + 0x2FF] : off - offset;
if (size > INSERTION_SORT_THRESHOLD) {
sort3(array, off - size, off, pointer);
} else if (size > 1) {
insertionSort(array, off - size, off);
}
}
}
public static void sort1(int[] array, int offset, int end, int[] pointer) {
Arrays.fill(pointer, 0, 0x100, 0);
for (int x = offset; x < end; ++x) {
++pointer[(array[x] >> 16) & 0xFF];
}
pointer[0] += offset;
pointer[0x200] = offset;
for (int x = 1; x < 0x100; ++x) {
int off = pointer[x - 1];
pointer[x + 0x200] = off;
pointer[x] += off;
}
for (int x = 0; x < 0x100; ++x) {
int off = pointer[x + 0x200];
while (off != pointer[x]) {
int value = array[off];
int y = (value >> 16) & 0xff;
if(x == y) {
off++;
} else {
while (x != y) {
int temp = array[pointer[y + 0x200]];
array[pointer[y + 0x200]++] = value;
value = temp;
y = (value >> 16) & 0xff;
}
array[off++] = value;
}
}
pointer[x + 0x200] = off;
}
for (int x = 0; x < 0x100; ++x) {
int off = pointer[x + 0x200];
int size = x > 0 ? off - pointer[x + 0x1FF] : off - offset;
if (size > INSERTION_SORT_THRESHOLD) {
sort2(array, off - size, off, pointer);
} else if (size > 1) {
insertionSort(array, off - size, off);
}
}
}
private static void sort0(int[] array, int offset, int end) {
int[] pointer = new int[0x500];
for (int x = offset; x < end; ++x) {
++pointer[(array[x] >> 24) + 0x80];
}
pointer[0] += offset;
pointer[0x100] = offset;
for (int x = 1; x < 0x100; ++x) {
int off = pointer[x - 1];
pointer[x + 0x100] = off;
pointer[x] += off;
}
for (int x = 0; x < 0x100; ++x) {
int off = pointer[x + 0x100];
while (off != pointer[x]) {
int value = array[off];
int y = (value >> 24) + 0x80;
if(x == y) {
off++;
} else {
while (x != y) {
int temp = array[pointer[y + 0x100]];
array[pointer[y + 0x100]++] = value;
value = temp;
y = (value >> 24) + 0x80;
}
array[off++] = value;
}
}
pointer[x + 0x100] = off;
}
for (int x = 0; x < 0x100; ++x) {
int off = pointer[x + 0x100];
int size = x > 0 ? off - pointer[x + 0xFF] : off - offset;
if (size > INSERTION_SORT_THRESHOLD) {
sort1(array, off - size, off, pointer);
} else if (size > 1) {
insertionSort(array, off - size, off);
}
}
}
public static void sort(int[] array, int offset, int end) {
if(offset < 0 || end < offset || end > array.length)
throw new IllegalArgumentException();
sort0(array, offset, end);
}
public static void sort(int[] array) {
sort0(array, 0, array.length);
}
private static void insertionSort(int array[], int offset, int end) {
for (int x = offset; x < end; ++x) {
for (int y = x; y > offset && array[y - 1] > array[y]; y--) {
int temp = array[y];
array[y] = array[y - 1];
array[y - 1] = temp;
}
}
}
public static void benchmark(int algorithm, int x) {
int y = (int) (Math.ceil(100000000d / x) + 0.5001d);
int[] array = new int[x*y];
Random r = new Random(1);
for (int i=0; i<array.length; ++i) {
array[i] = r.nextInt();
}
long a = System.nanoTime();
for (int i=0; i<y; ++i) {
if (algorithm == 0) sort(array, i * x, (i+1) * x); else Arrays.sort(array, i * x, (i+1) * x);
}
long b = System.nanoTime();
for (int i=0; i<y; ++i) {
int n = (i+1) * x - 1;
while (n --> i * x) {
if (array[n] > array[n+1]) {
throw new RuntimeException("oops");
}
}
}
long micro = ((b - a) / y) / 1000L;
System.out.println((algorithm == 0 ? "Radix.sort " : "Arrays.sort ") + micro);
}
public static void main(String[] args) {
benchmark(Integer.parseInt(args[0]), Integer.parseInt(args[1]));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment