@Test(timeout = TIMEOUT)
  public void testQuickSort() {
    DValue[] arrZero = new DValue[0];
    Sorting.quickSort(arrZero, new BasicComparator(), new Random());
    assertEquals(0, arrZero.length);
    DValue[] arrOne = new DValue[1];
    arrOne[0] = new DValue(4, 0);
    Sorting.quickSort(arrOne, new BasicComparator(), new Random());
    assertEquals(1, arrOne.length);
    assertEquals(4, arrOne[0].val.intValue());

    Random rand = new Random();
    HashMap<Integer, Integer> values = new HashMap<Integer, Integer>();
    for (int i = 0; i < 50; i++) {
      int arrlen = rand.nextInt(1000) + 2;
      DValue[] arrMany = new DValue[arrlen];
      DValue[] arrManySorted = new DValue[arrlen];
      for (int j = 0; j < arrlen; j++) {
        arrMany[j] = new DValue(rand.nextInt(200) - 90, 0);
        if (values.containsKey(arrMany[j].val)) {
          arrMany[j].count = values.get(arrMany[j].val) + 1;
          values.put(arrMany[j].val, arrMany[j].count);
        } else {
          values.put(arrMany[j].val, 0);
        }
      }
      System.arraycopy(arrMany, 0, arrManySorted, 0, arrlen);
      Arrays.sort(arrManySorted);
      Sorting.quickSort(arrMany, new BasicComparator(), new Random());
      assertArrayEquals(arrManySorted, arrMany);
    }
  }
  @Test(timeout = TIMEOUT)
  public void testRadixSort() {
    int[] arrZero = new int[0];
    arrZero = Sorting.radixSort(arrZero);
    assertEquals(0, arrZero.length);
    int[] arrOne = new int[1];
    arrOne[0] = 4;
    arrOne = Sorting.radixSort(arrOne);
    assertEquals(1, arrOne.length);
    assertEquals(4, arrOne[0]);

    Random rand = new Random();
    for (int i = 0; i < 50; i++) {
      int arrlen = rand.nextInt(1000) + 2;
      int[] arrMany = new int[arrlen];
      int[] arrManySorted = new int[arrlen];
      for (int j = 0; j < arrlen; j++) {
        arrMany[j] = rand.nextInt(200) - 90;
      }
      System.arraycopy(arrMany, 0, arrManySorted, 0, arrlen);
      Arrays.sort(arrManySorted);
      arrMany = Sorting.radixSort(arrMany);
      assertArrayEquals(arrManySorted, arrMany);
    }
  }
示例#3
0
  public static void main(String[] args) {
    int N = 10000000;
    int K = 1;
    Integer[] newArray = new Integer[N];
    StopWatch sw = new StopWatch();
    while (K <= N) {
      newArray = createArray(N, K);
      Sorting obj = new Sorting(newArray);
      newArray = (Integer[]) obj.selectionsort();

      System.out.println("The time needed for K=" + K + " is " + sw.elapsedTime());
      K = K * 2;
      System.out.println(
          "The number of comparisons is "
              + obj.compareCount()
              + "; "
              + "The number of exchanges is "
              + obj.exchangeCount()
              + "; "
              + "The number of copies is "
              + obj.copyCount()
              + "; ");
    }
  }