Skip to content

Instantly share code, notes, and snippets.

@Coderadish
Last active February 22, 2023 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Coderadish/e0b4fcce5774de0ec870ff69f815ed0d to your computer and use it in GitHub Desktop.
Save Coderadish/e0b4fcce5774de0ec870ff69f815ed0d to your computer and use it in GitHub Desktop.
ALG-002 - Bubblesort
public class Bubblesort {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int[] numbers = {1, 5, 8, 13, 17, 5, 9, 16, 25, 8, 0, 1};
int tmp;
for (int i = 1; i < numbers.length; i++) {
for (int j = 0; j < numbers.length - i; j++) {
if (numbers[j] > numbers[j + 1]) {
// swap elements in array
tmp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = tmp;
}
}
}
for (int number : numbers) {
System.out.print(number + ", ");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment