Skip to content

Instantly share code, notes, and snippets.

@LeoPFreitas
Last active August 6, 2020 16:06
Show Gist options
  • Save LeoPFreitas/753e9f15107ce700dbc23eda80768419 to your computer and use it in GitHub Desktop.
Save LeoPFreitas/753e9f15107ce700dbc23eda80768419 to your computer and use it in GitHub Desktop.
Simplest bubble sort algorithm implemented in Java.
public static int[] bubbleSort(int[] array) {
for (int i = 0; i < array.length - 1; i++) {
for (int j = 0; j < array.length - i - 1; j++) {
/* if a pair of adjacent elements has the wrong order it swaps them */
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
return array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment