Merge pull request #1357 from astralcake/master
Fixed wrong order in BubbleSort, corrected spelling mistakes
This commit is contained in:
commit
4b427845c1
@ -13,7 +13,7 @@ class BubbleSort implements SortAlgorithm {
|
|||||||
* This method implements the Generic Bubble Sort
|
* This method implements the Generic Bubble Sort
|
||||||
*
|
*
|
||||||
* @param array The array to be sorted
|
* @param array The array to be sorted
|
||||||
* Sorts the array in increasing order
|
* Sorts the array in ascending order
|
||||||
**/
|
**/
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -21,7 +21,7 @@ class BubbleSort implements SortAlgorithm {
|
|||||||
for (int i = 0, size = array.length; i < size - 1; ++i) {
|
for (int i = 0, size = array.length; i < size - 1; ++i) {
|
||||||
boolean swapped = false;
|
boolean swapped = false;
|
||||||
for (int j = 0; j < size - 1 - i; ++j) {
|
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);
|
swap(array, j, j + 1);
|
||||||
swapped = true;
|
swapped = true;
|
||||||
}
|
}
|
||||||
@ -41,12 +41,12 @@ class BubbleSort implements SortAlgorithm {
|
|||||||
BubbleSort bubbleSort = new BubbleSort();
|
BubbleSort bubbleSort = new BubbleSort();
|
||||||
bubbleSort.sort(integers);
|
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);
|
print(integers);
|
||||||
|
|
||||||
// String Input
|
// String Input
|
||||||
String[] strings = {"c", "a", "e", "b", "d"};
|
String[] strings = {"c", "a", "e", "b", "d"};
|
||||||
//Output => e, d, c, b, a
|
//Output => a, b, c, d, e
|
||||||
print(bubbleSort.sort(strings));
|
print(bubbleSort.sort(strings));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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 v first element
|
||||||
* @param w second 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 <T extends Comparable<T>> boolean less(T v, T w) {
|
static <T extends Comparable<T>> boolean less(T v, T w) {
|
||||||
return v.compareTo(w) < 0;
|
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 <T extends Comparable<T>> boolean greater(T v, T w) {
|
||||||
|
return v.compareTo(w) > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prints a list
|
||||||
*
|
*
|
||||||
* @param toPrint - a list which should be printed
|
* @param toPrint - a list which should be printed
|
||||||
*/
|
*/
|
||||||
@ -55,7 +67,7 @@ final class SortUtils {
|
|||||||
/**
|
/**
|
||||||
* Prints an array
|
* Prints an array
|
||||||
*
|
*
|
||||||
* @param toPrint - the array which should be printed
|
* @param toPrint - an array which should be printed
|
||||||
*/
|
*/
|
||||||
static void print(Object[] toPrint) {
|
static void print(Object[] toPrint) {
|
||||||
System.out.println(Arrays.toString(toPrint));
|
System.out.println(Arrays.toString(toPrint));
|
||||||
|
Loading…
Reference in New Issue
Block a user