@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]); }
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; }
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; } }
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; }  }
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); }
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);  }