示例#1
0
  public void testInsertionSort() {
    long before, after;
    System.out.println("Testing insertion sort.");
    for (int i = 0; i < 5; i++) {
      before = System.currentTimeMillis();
      sort.insertionSort(lists[i]);
      after = System.currentTimeMillis();

      System.out.println(after - before);
    }
  }
  @Test(timeout = TIMEOUT)
  public void testInsertionSort() {
    DValue[] arrZero = new DValue[0];
    Sorting.insertionSort(arrZero, new BasicComparator());
    assertEquals(0, arrZero.length);
    DValue[] arrOne = new DValue[1];
    arrOne[0] = new DValue(4, 0);
    Sorting.insertionSort(arrOne, new BasicComparator());
    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.insertionSort(arrMany, new BasicComparator());
      assertArrayEquals(arrManySorted, arrMany);
      for (int j = 0; j < arrMany.length - 1; j++) {
        if (arrMany[j].val.equals(arrMany[j + 1].val)) {
          assertTrue(arrMany[j].count < arrMany[j + 1].count);
        }
      }
    }
  }
示例#3
0
 private void btninActionPerformed(
     java.awt.event.ActionEvent evt) { // GEN-FIRST:event_btninActionPerformed
   disbutton();
   list.clear();
   start = System.currentTimeMillis();
   insertionSort(num);
   end = System.currentTimeMillis();
   time = (end - start);
   if (time >= 1000) {
     time = time / 1000;
     lblbub.setText("" + time + " Seconds");
   } else {
     lblbub.setText("" + time + " Milliseconds");
   }
   for (int x = 1; x <= amnt; x++) {
     list.addElement(num[x]);
   }
 } // GEN-LAST:event_btninActionPerformed