Last active
August 6, 2020 16:06
-
-
Save LeoPFreitas/753e9f15107ce700dbc23eda80768419 to your computer and use it in GitHub Desktop.
Simplest bubble sort algorithm implemented in Java.
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
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