コード例 #1
0
ファイル: SortTest.java プロジェクト: realnumber/cs206
 @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);
 }
コード例 #2
0
ファイル: SortTest.java プロジェクト: realnumber/cs206
 @Test
 public void testQuickSortDescending() {
   QuickSort qs = new QuickSort(list);
   int[] result = qs.sort(QuickSort.ASC_DESC.DESCENDING);
   for (int i = 0; i < list.length; i++) assertEquals(list.length - 1 - i, result[i]);
 }
コード例 #3
0
 @Test
 public void testSort() throws Exception {
   quickSort.sort(array, 0, 7);
   assertArrayEquals(expected, array);
 }
コード例 #4
0
ファイル: SortTest.java プロジェクト: realnumber/cs206
  @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'.
  }
コード例 #5
0
 @Test
 public void testQuickSort() {
   List<Integer> list = getRandomUnsortedList();
   testSort(list, QuickSort.sort(list));
 }