📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Java Program to Implement Bubble Sort in Ascending Order
Below is the complete Java program that demonstrates the Bubble Sort algorithm for sorting an array in ascending order, followed by its output.
public class BubbleSortAscending {
public static void main(String[] args) {
// Define a sample array of numbers
int[] numbers = {34, 12, 56, 78, 33};
// Sort the array using bubble sort
bubbleSortAscending(numbers);
// Display the sorted array
System.out.println("Sorted array in ascending order:");
for (int num : numbers) {
System.out.print(num + " ");
}
}
/**
* Sorts an array of integers in ascending order using Bubble Sort.
*
* @param arr The array to be sorted.
*/
public static void bubbleSortAscending(int[] arr) {
int n = arr.length; // Get the length of the array
boolean swapped; // Flag to track whether elements were swapped in a pass
// Outer loop for the number of passes
for (int i = 0; i < n - 1; i++) {
swapped = false; // Reset the swapped flag for each pass
// Inner loop to compare and potentially swap adjacent elements
for (int j = 0; j < n - i - 1; j++) {
// If the current element is greater than the next element
if (arr[j] > arr[j + 1]) {
// Swap the two elements
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
// Set the swapped flag to true as a swap has been performed
swapped = true;
}
}
// If no elements were swapped in the inner loop, the array is sorted
if (!swapped) break;
}
}
}
Output:
Sorted array in ascending order:
12 33 34 56 78
Understanding the Code
- The outer loop represents the pass through the array.
- The inner loop performs the actual comparisons and potential swaps.
- If the current element (arr[j]) is greater than the next element (arr[j+1]), they are swapped to ensure that numbers are sorted in ascending order.
- The swapped flag acts as an optimization. If during a full pass, no swaps are made, it indicates the array is sorted, and we break out of the loop.
Key Takeaways
Conclusion
Related Java Programs on Sorting Algorithms
- Bubble Sort in Ascending Order in Java
- Bubble Sort in Descending Order in Java
- Selection Sort in Ascending Order in Java
- Selection Sort in Descending Order in Java
- Insertion Sort in Ascending Order in Java
- Insertion Sort in Descending Order in Java
- Merge Sort in Ascending Order in Java
- Merge Sort in Descending Order in Java
- Quick Sort in Ascending Order in Java
- Quick Sort in Descending Order in Java
Comments
Post a Comment
Leave Comment