Skip to content

Commit 4b42784

Browse files
authored
Merge pull request TheAlgorithms#1357 from astralcake/master
Fixed wrong order in BubbleSort, corrected spelling mistakes
2 parents 88d65ff + 95389bf commit 4b42784

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

Sorts/BubbleSort.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ class BubbleSort implements SortAlgorithm {
1313
* This method implements the Generic Bubble Sort
1414
*
1515
* @param array The array to be sorted
16-
* Sorts the array in increasing order
16+
* Sorts the array in ascending order
1717
**/
1818

1919
@Override
2020
public <T extends Comparable<T>> T[] sort(T[] array) {
2121
for (int i = 0, size = array.length; i < size - 1; ++i) {
2222
boolean swapped = false;
2323
for (int j = 0; j < size - 1 - i; ++j) {
24-
if (less(array[j], array[j + 1])) {
24+
if (greater(array[j], array[j + 1])) {
2525
swap(array, j, j + 1);
2626
swapped = true;
2727
}
@@ -41,12 +41,12 @@ public static void main(String[] args) {
4141
BubbleSort bubbleSort = new BubbleSort();
4242
bubbleSort.sort(integers);
4343

44-
// Output => 231, 78, 54, 23, 12, 9, 6, 4, 1
44+
// Output => 1, 4, 6, 9, 12, 23, 54, 78, 231
4545
print(integers);
4646

4747
// String Input
4848
String[] strings = {"c", "a", "e", "b", "d"};
49-
//Output => e, d, c, b, a
49+
//Output => a, b, c, d, e
5050
print(bubbleSort.sort(strings));
5151

5252
}

Sorts/SortUtils.java

+16-4
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,31 @@ static <T> boolean swap(T[] array, int idx, int idy) {
2626

2727

2828
/**
29-
* This method checks if first element is less then the other element
29+
* This method checks if first element is less than the other element
3030
*
3131
* @param v first element
3232
* @param w second element
33-
* @return true if the first element is less then the second element
33+
* @return true if the first element is less than the second element
3434
*/
3535
static <T extends Comparable<T>> boolean less(T v, T w) {
3636
return v.compareTo(w) < 0;
3737
}
3838

3939

4040
/**
41-
* Just print list
41+
* This method checks if first element is greater than the other element
42+
*
43+
* @param v first element
44+
* @param w second element
45+
* @return true if the first element is greater than the second element
46+
*/
47+
static <T extends Comparable<T>> boolean greater(T v, T w) {
48+
return v.compareTo(w) > 0;
49+
}
50+
51+
52+
/**
53+
* Prints a list
4254
*
4355
* @param toPrint - a list which should be printed
4456
*/
@@ -55,7 +67,7 @@ static void print(List<?> toPrint) {
5567
/**
5668
* Prints an array
5769
*
58-
* @param toPrint - the array which should be printed
70+
* @param toPrint - an array which should be printed
5971
*/
6072
static void print(Object[] toPrint) {
6173
System.out.println(Arrays.toString(toPrint));

0 commit comments

Comments
 (0)