Bubble Sort in Java: Explanation & Practice
Implement bubble sort algorithm
Problem summary
Write a program that sorts an array using bubble sort algorithm.
Starter code
public class Main {
public static void main(String[] args) {
int[] arr = {64, 34, 25, 12, 22, 11, 90}; // Test case 1
// Implement bubble sort
// Print sorted array space-separated
}
}Expected output and test cases
- Sort array
11 12 22 25 34 64 90
- Already sorted stays same
1 2 3 4 5
- Reverse sorted becomes sorted
1 2 3 4 5
Hints
- Compare adjacent elements
- Swap if in wrong order
- Repeat until no swaps needed
Related Arrays exercises
Practice all Arrays exercises · Run this idea in the Java compiler