/* * [TestQuickSort.java] * * Summary: demonstrate how to use QuickSort 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 * Eric van Bezooijen was the * riginal author of this version of QuickSort. I modified * he version he posted to comp.lang.java to use a callback * elegate object. I also made a few optimisations. * have also posted source for HeapSort and RadixSort both * of which run faster than QuickSort. */ package com.mindprod.quicksort; import java.util.Comparator; import java.util.Random; import static java.lang.System.*; /** * demonstrate how to use QuickSort class. *

* java.exe com.mindprod.quicksort.TestQuickSort * * @author Roedy Green, Canadian Mind Products * @version 1.6 2008-01-01 add generics to comparator * Eric van Bezooijen was the * riginal author of this version of QuickSort. I modified * he version he posted to comp.lang.java to use a callback * elegate object. I also made a few optimisations. * have also posted source for HeapSort and RadixSort both * f which run faster than QuickSort. * @since 1996 */ public class TestQuickSort { // Test QuickSort 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 QuickSort items=" + N ); long start = System.currentTimeMillis(); QuickSort.sort( anArray, new StringComparator() ); 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 /** * Sort alphabetically, case-sensitive. *

* Defines an alternate sort order for String. * * @author Roedy Green, Canadian Mind Products * @version 1.1 2009-04-13 - add comments * @since 2002-05-21 */ private static class StringComparator implements Comparator { /** * Sort alphabetically, case-sensitive. * Defines an alternate sort order for String. * Compare two String Objects. * 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 StringComparator } // end class TestQuickSort