Skip to content

Instantly share code, notes, and snippets.

@connor-davis
Created July 25, 2022 12:30
Show Gist options
  • Save connor-davis/eaa79d314c638f789b4ba4c63ebaaeda to your computer and use it in GitHub Desktop.
Save connor-davis/eaa79d314c638f789b4ba4c63ebaaeda to your computer and use it in GitHub Desktop.
Java Bubble Sort Algorithm
/**
* 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