/* * [TestShellSort.java] * * Summary: Example of how you might use the ShellSort class to sort an array of Strings. * * Copyright: (c) 1998-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.4 2008-01-01 add generics */ package com.mindprod.shellsort; import java.util.Comparator; import java.util.Random; import static java.lang.System.*; /** * Example of how you might use the ShellSort class to sort an array of Strings. *

* Invoke with java.exe com.mindprod.shellsort.TestShellSort * * @author Roedy Green, Canadian Mind Products * @version 1.4 2008-01-01 add generics * @since 1998 */ public class TestShellSort { // Test ShellSort 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 ShellSort items=" + N ); long start = System.currentTimeMillis(); ShellSort.sort( anArray, new AlphabeticallyCaseSensitive() ); 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 /** * Case sensitive String compare. *

* Defines an alternate sort order for String. * * @version 1.0 2009-05-22 - initial release * @since 2009-05-22 */ private static class AlphabeticallyCaseSensitive implements Comparator { /** * Case sensitive String compare. * Defines an alternate sort order for String. * Compare two String Objects. * Compares x. * Informally, returns (a-b), or +ve if a is more positive than b. * * @param a first String to compare * @param b second String to compare * * @return +ve if a>b, 0 if a==b, -ve if a<b */ public final int compare( String a, String b ) { return a.compareTo( b ); } } } // end class TestShellSort