@Test // The following test is on a relatively large unsorted list filled with random numbers allowing // duplicated elements. public void testAllSecond() { // Make a random target list. int s = 1000; int[] target = new int[s]; Random rand = new Random(System.currentTimeMillis()); for (int i = 0; i < target.length; i++) target[i] = rand.nextInt(s / 2); // Sort the resulting list with each sorting algorithm. MergeSort ms = new MergeSort(target); QuickSort qs = new QuickSort(target); HeapSort hs = new HeapSort(target); int[] mr = ms.sort(MergeSort.ASC_DESC.DESCENDING); int[] qr = qs.sort(QuickSort.ASC_DESC.DESCENDING); int[] hr = hs.sort(HeapSort.ASC_DESC.DESCENDING); // Compare the results. /*for (int i = 0; i < target.length; i++) { assertEquals(mr[i], qr[i]); assertEquals(qr[i], hr[i]); }*/ assertArrayEquals(mr, qr); assertArrayEquals(qr, hr); }
/** @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int[] num = readNumberFromCommandLine(); System.out.println("NUMBERS ENTERED TO SORT"); for (int i = 0; i < num.length; i++) { System.out.print(num[i] + "\t"); } /*Call for Insertion Sorting *Uncomment the below line to execute merge sort * */ // InsertionSort.insertionSort(num); /*Call for Merge Sorting *Uncomment the below line to execute merge sort * */ // MergeSort.sort(num); /*Call for Bubble Sorting *Uncomment the below line to execute merge sort * */ // BubbleSort.sort(num); /*Call for Heap Sorting *Uncomment the below line to execute merge sort * */ HeapSort.sort(num); }
public static void main(String[] args) { // Array length int length = 20000; // Random number range int range = 200; // Print results boolean print = false; // For measuring performance long startTime, endTime; int[] unsortedArray = new int[length]; // Array of randomly generated numbers Random generator = new Random(); for (int i = 0; i < unsortedArray.length; i++) { unsortedArray[i] = generator.nextInt(range); } // BubbleSort run startTime = System.nanoTime(); int[] bubbleSortedArray = BubbleSort.sort(unsortedArray); endTime = System.nanoTime(); System.out.println("BubbleSort " + (endTime - startTime)); // SelectionSort run startTime = System.nanoTime(); int[] selectionSortedArray = SelectionSort.sort(unsortedArray); endTime = System.nanoTime(); System.out.println("SelectionSort " + (endTime - startTime)); // HeapSort run startTime = System.nanoTime(); int[] heapSortedArray = HeapSort.sort(unsortedArray); endTime = System.nanoTime(); System.out.println("HeapSort " + (endTime - startTime)); // Print the arrays if (print) { Helper.printArray(unsortedArray); Helper.printArray(bubbleSortedArray); Helper.printArray(heapSortedArray); Helper.printArray(selectionSortedArray); } }
@Test public void testHeapSortDescending() { HeapSort hs = new HeapSort(list); int[] result = hs.sort(HeapSort.ASC_DESC.DESCENDING); for (int i = 0; i < list.length; i++) assertEquals(list.length - 1 - i, result[i]); }
@Test public void testPerformance() { // Make a random target list. int s = 50000; int[] target = new int[s]; Random rand = new Random(System.currentTimeMillis()); for (int i = 0; i < target.length; i++) target[i] = rand.nextInt(s / 2); // Sort the resulting list with each sorting algorithm. MergeSort ms = new MergeSort(target); QuickSort qs = new QuickSort(target); HeapSort hs = new HeapSort(target); BubbleSort bs = new BubbleSort(target); InsertionSort is = new InsertionSort(target); long start; start = System.currentTimeMillis(); int[] mr = ms.sort(MergeSort.ASC_DESC.DESCENDING); long mt = System.currentTimeMillis() - start; start = System.currentTimeMillis(); int[] qr = qs.sort(QuickSort.ASC_DESC.DESCENDING); long qt = System.currentTimeMillis() - start; start = System.currentTimeMillis(); int[] hr = hs.sort(HeapSort.ASC_DESC.DESCENDING); long ht = System.currentTimeMillis() - start; start = System.currentTimeMillis(); int[] br = bs.sort(BubbleSort.ASC_DESC.DESCENDING); long bt = System.currentTimeMillis() - start; start = System.currentTimeMillis(); int[] ir = is.sort(InsertionSort.ASC_DESC.DESCENDING); long it = System.currentTimeMillis() - start; // Compare the results. /* for (int i = 0; i < target.length; i++) { assertEquals(mr[i], qr[i]); assertEquals(qr[i], hr[i]); assertEquals(hr[i], br[i]); assertEquals(br[i], ir[i]); }*/ assertArrayEquals(mr, qr); assertArrayEquals(qr, hr); assertArrayEquals(hr, br); assertArrayEquals(br, ir); System.out.println( "SortTest.testPerformance(): MS " + mt + ", QS " + qt + ", HS " + ht + ", BS " + bt + ", IS " + it); // Analyze the performance result. assertTrue(mt < bt && mt < it); assertTrue(qt < bt && qt < it); assertTrue(ht < bt && ht < it); assertTrue( it < bt); // Check whether your system does the same with TA's, since insertion sort is // faster in TA's machine. // Analyze with more tight conditions. double factor = 0.25; assertTrue(mt < factor * it); assertTrue(qt < factor * it); assertTrue(ht < factor * it); // FYI, in TA's machine, it is like 'MS 41, QS 216, HS 19, BS 6146, IS 1942'. }