Last active
February 22, 2023 13:40
-
-
Save Coderadish/e0b4fcce5774de0ec870ff69f815ed0d to your computer and use it in GitHub Desktop.
ALG-002 - Bubblesort
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 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