示例#1
0
  @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'.
  }
示例#2
0
 @Test
 public void testInsertionSort() {
   List<Integer> list = getRandomUnsortedList();
   testSort(list, InsertionSort.sort(list));
 }