import java.util.*; /* Implementation of (counting based) radix sort for 32-bit integers. * * To run performance test: java -Xms32m -Xmx256m RadixSorting * @author Rasmus Pagh 2009 */ class GoodRadixSorting { public static void radixSort(int[] A){ // Radixsort for 32 bit integers, using 2 passes // Uses counting to simplify memory allocation // Precondition: Array has only positive integers int n=A.length; int[] countL = new int[65536]; // Low character counts int[] countH = new int[65536]; // High character counts int[] tmp = new int[n]; // Temporary for result of first pass // Count character occurrences, low and high bits: for (int i=0; i>16]++; } // Compute prefix sums ("memory allocation"): for (int i=1; i<65536; i++) { countL[i]+=countL[i-1]; countH[i]+=countH[i-1]; } // Sort by least significant char. A->tmp for (int i=0; iA (backwards for stability) for (int i=n-1; i>=0; i--) A[--countH[tmp[i]>>16]]=tmp[i]; } public static void main(String[] args) { for (int r=256; r<=33554432; r=r*2) { int[] T=new int[r]; Random rand = new Random(System.currentTimeMillis()); for (int i=0; i