Created
July 25, 2022 12:30
-
-
Save connor-davis/eaa79d314c638f789b4ba4c63ebaaeda to your computer and use it in GitHub Desktop.
Java Bubble Sort Algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This method will use the bubble sort algorithm to sort an int[] of numbers. | |
* | |
* @param numbers | |
* | |
* @return int[] | |
*/ | |
public static int[] bubbleSort(int[] numbers) { | |
for (int x = 0; x < numbers.length; x++) { | |
for (int i = 0; i < numbers.length - 1; i++) { | |
int a = numbers[i]; | |
int b = numbers[i + 1]; | |
if (a > b) { | |
numbers[i] = b; | |
numbers[i + 1] = a; | |
} | |
} | |
} | |
return numbers; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment