### Question:
OptimizedBubbleSort { public <T> int sort(T[] array, Comparator<T> comparator, boolean optimized) { if (array == null) { return 0; } int counter = 0; boolean swapped = true; int lastSwap = array.length-1; while (swapped) { swapped = false; int limit = array.length - 1; if(optimized){ limit = lastSwap; } for (int i = 0; i < limit; i++) { int j = i + 1; counter++; if (comparator.compare(array[i], array[j]) > 0) { T tmp = array[i]; array[i] = array[j]; array[j] = tmp; swapped = true; lastSwap = i; } } } return counter; }  int sort(T[] array, Comparator<T> comparator, boolean optimized);  }### Answer:
@Test public void testNull(){ int comparisons = sorter.sort(null, new StringComparator(), false); assertEquals(0, comparisons); }
@Test public void testAlreadySorted(){ String[] array = {"a", "b", "c", "d"}; int comparisons = sorter.sort(array, new StringComparator(), false); assertEquals(3, comparisons); assertEquals("a", array[0]); assertEquals("b", array[1]); assertEquals("c", array[2]); assertEquals("d", array[3]); }
@Test public void testInverted(){ String[] array = {"d", "c", "b", "a"}; int comparisons = sorter.sort(array, new StringComparator(), false); assertEquals(12, comparisons); assertEquals("a", array[0]); assertEquals("b", array[1]); assertEquals("c", array[2]); assertEquals("d", array[3]); }
@Test public void testBase(){ String[] array = {"d", "a", "c", "b"}; sorter.sort(array, new StringComparator(), false); assertEquals("a", array[0]); assertEquals("b", array[1]); assertEquals("c", array[2]); assertEquals("d", array[3]); }
@Test public void testNearly(){ String[] array = {"c", "b", "a", "d", "e", "f"}; int optimized = sorter.sort(array, new StringComparator(), true); array = new String[]{"c", "b", "a", "d", "e", "f"}; int base = sorter.sort(array, new StringComparator(), false); assertTrue(optimized < base); assertTrue(optimized < base/2); assertEquals("a", array[0]); assertEquals("b", array[1]); assertEquals("c", array[2]); assertEquals("d", array[3]); assertEquals("e", array[4]); assertEquals("f", array[5]); }
@Test public void testGameUsers(){ GameUser a = new GameUser("a", 10); GameUser b = new GameUser("b", 5); GameUser c = new GameUser("c", 2); GameUser d = new GameUser("d", 5); GameUser[] array = {a,b,c,d}; sorter.sort(array, new GameUserComparator(), true); assertEquals("c", array[0].getUserId()); assertEquals("b", array[1].getUserId()); assertEquals("d", array[2].getUserId()); assertEquals("a", array[3].getUserId()); }