/* * [TestHeapSort.java] * * Summary: Test Heapsort, demonstrate its use. * * 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.7 2008-01-01 add generics to Comparator */ /** * Test Heapsort, demonstrate its use. *

* Example of how you might use the HeapSort class to sort an array of * Strings. *

* invoke with: * c: * cd \ * java.exe com.mindprod.heapsort.TestHeapSort *

* E:\intellij\j5m\src\com\mindprod\heapsort\TestHeapSort.java * * @author Roedy Green, Canadian Mind Products * @version 1.7 2008-01-01 add generics to Comparator * @since 1996 */ package com.mindprod.heapsort; import java.util.Comparator; import java.util.Random; import static java.lang.System.*; public class TestHeapSort { // Test HeapSort by sorting N random Strings public static void main( String[] args ) { 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 HeapSort items=" + N ); long start = System.currentTimeMillis(); HeapSort.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. */ 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 TestHeapSort