Implement the Bubble Sort algorithm in Java to sort an array in ascending order.
Problem:
Given an array of integers, repeatedly swap adjacent elements if they are in the wrong order until the array is sorted.
Learning Goals:
- Understand Bubble Sort
- Practice nested loops
- Learn swapping techniques
- Optimize Bubble Sort using a swap flag
- Analyze time complexity O(n²)
Example:
Input:
5 1 4 2 8
Output:
1 2 4 5 8
Implement the Bubble Sort algorithm in Java to sort an array in ascending order.
Problem:
Given an array of integers, repeatedly swap adjacent elements if they are in the wrong order until the array is sorted.
Learning Goals:
Example:
Input:
5 1 4 2 8
Output:
1 2 4 5 8