Skip to content
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

Improved Bubble Sort Algorithm #215

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
56 changes: 31 additions & 25 deletions src/main/java/algorithm/BubbleSortSnippet.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,35 @@
* SOFTWARE.
*/

package algorithm;
package algorithm;

/**
* BubbleSortSnippet.
*/
public class BubbleSortSnippet {

/**
* Sort an array with bubbleSort algorithm.
*
* @param arr array to sort
*/
public static void bubbleSort(int[] arr) {
var lastIndex = arr.length - 1;

for (var j = 0; j < lastIndex; j++) {
for (var i = 0; i < lastIndex - j; i++) {
if (arr[i] > arr[i + 1]) {
var tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
}
}
}
}
}
/**
* BubbleSortSnippet.
*/
public class BubbleSortSnippet {

/**
* Sort an array using the bubble sort algorithm.
*
* @param arr array to sort
*/
public static void bubbleSort(int[] arr) {
int lastIndex = arr.length - 1;
boolean sorted;

do {
sorted = true;
for (int i = 0; i < lastIndex; i++) {
if (arr[i] > arr[i + 1]) {
int tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
sorted = false;
}
}

lastIndex--;

} while (!sorted);
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add empty line at EOF to satisfy github

Loading