🎓 Check Out My Top 25 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare
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
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment