Example #1
0
        public int getPivotIndex(MyArrayList a, int start, int end) {
          int first = (Integer) a.get(start);
          int second = (Integer) a.get((start + end) / 2);
          int third = (Integer) a.get(end);
          int firstInd = start;
          int secondInd = (start + end) / 2;
          int thirdInd = end;

          if (first <= second) {
            if (third <= first) {
              return firstInd;
            } else {
              if (third >= second) {
                return secondInd;
              } else {
                return thirdInd;
              }
            }
          } else {
            if (third >= first) {
              return firstInd;
            } else {
              if (third <= second) {
                return secondInd;
              } else {
                return thirdInd;
              }
            }
          }
        }
 /** Moo */
 @Test
 public void testLargeAdd() {
   for (int i = 0; i < 10000; i++) {
     words.add("" + i);
   }
   assertEquals(10000, words.size());
   assertEquals("0", words.get(0));
   assertEquals("9999", words.get(9999));
 }
 /** Moo */
 @Test
 public void testAddLoc() {
   words.add(0, "5");
   System.out.println(words.toString());
   words.add(0, "3");
   System.out.println(words.toString());
   words.add(1, "4");
   System.out.println(words.toString());
   assertSame("3", words.get(0));
   assertSame("4", words.get(1));
   assertSame("5", words.get(2));
   assertEquals(3, words.size());
 }
  /** Moo */
  @Test
  public void testAdd() {
    assertTrue(words.add("hello"));
    assertEquals(1, words.size());
    assertTrue(words.add("world"));
    assertEquals(2, words.size());
    assertTrue(words.add("blue"));
    assertEquals(3, words.size());

    assertSame("hello", words.get(0));
    assertSame("world", words.get(1));
    assertSame("blue", words.get(2));
  }
 /** Moo */
 @Test
 public void testGet() {
   words.add("moo");
   words.add("cow");
   assertSame("moo", words.get(0));
   assertSame("cow", words.get(1));
 }
 /** Moo */
 @Test(expected = IndexOutOfBoundsException.class)
 public void testGetFail3() {
   assertEquals(0, words.size());
   words.add("0");
   words.add("0");
   words.add("0");
   words.get(3);
 }
  /** Moo */
  @Test
  public void testSet() {

    words.add("6");
    words.add("7");
    assertEquals("6", words.set(0, "3"));
    assertEquals("3", words.get(0));
    assertEquals("7", words.get(1));
    assertEquals(2, words.size());
  }
Example #8
0
 private static void quickSortClassic(
     MyArrayList a, int start, int end, IPivotStrategy pivotStrategy) {
   if (end - start < 1) {
     return;
   }
   int pivotIndex = pivotStrategy.getPivotIndex(a, start, end);
   int pivotValue = (Integer) a.get(pivotIndex);
   swap(a, start, pivotIndex);
   int i = start + 1;
   int j = start + 1;
   while (j <= end) {
     if ((Integer) a.get(j) < pivotValue) {
       swap(a, j, i);
       i++;
     }
     j++;
   }
   swap(a, start, i - 1);
   quickSortClassic(a, start, i - 2, pivotStrategy);
   quickSortClassic(a, i, end, pivotStrategy);
 }
Example #9
0
  private static int binarySearch(MyArrayList list, int start, int end, Object key) {

    int index = start + (end - start) / 2;

    int compare = ((Integer) key).compareTo((Integer) list.get(index));

    if (compare < 0) {
      if (start == index) {
        return -1 * index - 1;
      }
      index = binarySearch(list, start, index - 1, key);
    }
    if (compare > 0) {
      if (end == index) {
        return -1 * (index + 1) - 1;
      }
      index = binarySearch(list, index + 1, end, key);
    }
    return index;
  }
Example #10
0
 @Override
 public T next() {
   if (!hasNext()) throw new NoSuchElementException();
   return hashMap.lookup(keys.get(current++));
 }