Skip to content

Fixed wrong order in BubbleSort, corrected spelling mistakes #1357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from Jul 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Sorts/BubbleSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ 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
public <T extends Comparable<T>> T[] sort(T[] array) {
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;
}
Expand All @@ -41,12 +41,12 @@ public static void main(String[] args) {
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));

}
Expand Down
20 changes: 16 additions & 4 deletions Sorts/SortUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,31 @@ static <T> boolean swap(T[] array, int idx, int idy) {


/**
* 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 <T extends Comparable<T>> boolean less(T v, T w) {
return v.compareTo(w) < 0;
}


/**
* 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
*/
Expand All @@ -55,7 +67,7 @@ static void print(List<?> toPrint) {
/**
* 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));
Expand Down