🎓 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 (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this blog post, we will learn how to write a Java Program to Implement Bubble Sort in Ascending Order.
Bubble sort is a sorting algorithm that compares two adjacent elements and swaps them until they are in the intended order. Just like the movement of air bubbles in the water that rises up to the surface, each element of the array moves to the end in each iteration. Therefore, it is called a bubble sort.
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
Performance: Bubble Sort has an average and worst-case time complexity of O(n^2). Therefore, while it’s great for learning and can be efficient for small datasets, there are more efficient sorting algorithms for larger datasets.
Learning Value: Grasping the logic behind Bubble Sort can make understanding more complex algorithms easier.
Conclusion
Bubble Sort in ascending order in Java offers beginners a concise and intuitive introduction to the world of sorting algorithms. Although there are numerous other, more efficient algorithms out there, starting with Bubble Sort allows for a robust foundation in algorithmic thought. Dive in, experiment, and then explore further into the fascinating realm of algorithms!
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
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Comments
Post a Comment
Leave Comment