/* * [TestRadixSort.java] * * Summary: Demonstrate how to use the RadixSort class. * * Copyright: (c) 1996-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.6 2008-01-01 add generics to Comparator */ package com.mindprod.radixsort; import java.util.Random; import static java.lang.System.*; /** * Demonstrate how to use the RadixSort class. * * @author Roedy Green, Canadian Mind Products * @version 1.6 2008-01-01 add generics to Comparator * @since 1996 */ public class TestRadixSort { // Test RadixSort by sorting N random Strings public static void main( String[] args ) { final int N = 100000; String[] anArray = new String[ N ]; Random wheel = new Random( 149 ); for ( int i = 0; i < anArray.length; i++ ) { // keys of form A9999 anArray[ i ] = "A" + ( ( wheel.nextInt() & Integer.MAX_VALUE ) % 10000 ); } out.println( "Start RadixSort items=" + N ); long start = System.currentTimeMillis(); RadixSort.sort( anArray, new Latin1Comparator(), 5 ); long stop = System.currentTimeMillis(); out.println( "Elapsed:" + ( stop - start ) ); // waste a little time to let user admire the results try { Thread.sleep( 3000 ); } catch ( InterruptedException e ) { } } // end main } // end class TestRadixSort // Callback delegate to describe collating sequence // to RadixSort class Latin1Comparator implements RadixComparator { // Comparator two Strings. Callback for sort. // effectively returns a-b; // e.g. +1 (or any +ve number) if a > b // 0 if a == b // -1 (or any -ve number) if a < b public final int compare( String a, String b ) { return a.compareTo( b ); } // end compare /* compare comparators */ public final boolean equals( Object a, Object b ) { return ( a == b ); } // end equals public final int getKeyByteAt( String a, int offset ) { if ( offset >= a.length() ) { return 0; } // only use low order 8 bits in sort key return a.charAt( offset ) & 0xff; } // end getKeyByteAt } // end class Latin1Comparator