diff --git a/Sorts/BubbleSort.java b/Sorts/BubbleSort.java index 9d2f3717..7df1fae8 100644 --- a/Sorts/BubbleSort.java +++ b/Sorts/BubbleSort.java @@ -13,7 +13,7 @@ class BubbleSort implements SortAlgorithm { * This method implements the Generic Bubble Sort * * @param array The array to be sorted - * Sorts the array in increasing order + * Sorts the array in ascending order **/ @Override @@ -21,7 +21,7 @@ class BubbleSort implements SortAlgorithm { for (int i = 0, size = array.length; i < size - 1; ++i) { boolean swapped = false; for (int j = 0; j < size - 1 - i; ++j) { - if (less(array[j], array[j + 1])) { + if (greater(array[j], array[j + 1])) { swap(array, j, j + 1); swapped = true; } @@ -41,12 +41,12 @@ class BubbleSort implements SortAlgorithm { BubbleSort bubbleSort = new BubbleSort(); bubbleSort.sort(integers); - // Output => 231, 78, 54, 23, 12, 9, 6, 4, 1 + // Output => 1, 4, 6, 9, 12, 23, 54, 78, 231 print(integers); // String Input String[] strings = {"c", "a", "e", "b", "d"}; - //Output => e, d, c, b, a + //Output => a, b, c, d, e print(bubbleSort.sort(strings)); } diff --git a/Sorts/SortUtils.java b/Sorts/SortUtils.java index d5f592ad..334e3c1c 100644 --- a/Sorts/SortUtils.java +++ b/Sorts/SortUtils.java @@ -26,11 +26,11 @@ final class SortUtils { /** - * This method checks if first element is less then the other element + * This method checks if first element is less than the other element * * @param v first element * @param w second element - * @return true if the first element is less then the second element + * @return true if the first element is less than the second element */ static > boolean less(T v, T w) { return v.compareTo(w) < 0; @@ -38,7 +38,19 @@ final class SortUtils { /** - * Just print list + * This method checks if first element is greater than the other element + * + * @param v first element + * @param w second element + * @return true if the first element is greater than the second element + */ + static > boolean greater(T v, T w) { + return v.compareTo(w) > 0; + } + + + /** + * Prints a list * * @param toPrint - a list which should be printed */ @@ -55,7 +67,7 @@ final class SortUtils { /** * Prints an array * - * @param toPrint - the array which should be printed + * @param toPrint - an array which should be printed */ static void print(Object[] toPrint) { System.out.println(Arrays.toString(toPrint));