import java.util.Scanner;
class BubbleSort {
public static void main(String[] args) {
int n, c, d, swap;
Scanner in = new Scanner(System.in);
System.out.println("Input number of integers to sort");
n = in.nextInt();
int array[] = new int[n];
System.out.println("Enter " + n + " integers");
for (c = 0; c < n; c++) {
array[c] = in.nextInt();
}
for (c = 0; c < (n - 1); c++) {
for (d = 0; d < n - c - 1; d++) {
if (array[d] > array[d + 1]) // For descending order use <
{
swap = array[d];
array[d] = array[d + 1];
array[d + 1] = swap;
}
}
}
System.out.println("Sorted list of numbers");
for (c = 0; c < n; c++) {
System.out.println(array[c]);
}
}
}
This program performs the Bubble Sort algorithm on an integer array entered by the user.
Step - by - Step Explanation:
1. Importing the Scanner Class:
import java.util.Scanner;
The Scanner class is imported to take the input from the user.
2. Declaring the Class:
class BubbleSort {
The program lies in the BubbleSort class.
3. Main Method:
public static void main(String[] args) {
This is the entry point
for execution.In short, this is the starting point of the program.
4. Declare Variables:
int n, c, d, swap;
n: Number of integers to be sorted by the user.
c and d: Counter variables
for traversing the array.
swap: Temporary variable to swap two elements of the array
while sorting.
5. Scanner Object Creation:
Scanner in = new Scanner(System.in);
A Scanner object in is defined to read from the user.
6. Input Number of Integers:
System.out.println("Input number of integers to sort");
n = in.nextInt();
The program prompts the user to enter the number of integers n they intend to sort.
7. Create the Array:
int array[] = new int[n];
An array array[] of integers is declared with a size of n, where n is the number of integers the user wants to input.
8. Input the Array Elements:
System.out.println("Enter " + n + " integers");
for (c = 0; c < n; c++) array[c] = in.nextInt();
It asks the user to input n integers.Then, using a
for loop, the entered values are stored in the array[].
9. Algorithm
for Bubble Sort
for (c = 0; c < (n - 1); c++) {
for (d = 0; d < n - c - 1; d++) {
if (array[d] > array[d + 1]) {
swap = array[d];
array[d] = array[d + 1];
array[d + 1] = swap;
}
}
}
- The outer for loop runs n – 1 times on the array since in the worst case, the largest element would take n – 1 iterations to bubble up to the end of the array.
- The inner for loop compares adjacent elements of the array.
- If the element at index d is larger than the element at index d + 1, they are interchanged.
- This interchange is accomplished using a temporary variable swap.
- This process “bubbles” the larger elements towards the end of the array.
10. Print the Sorted Array:
System.out.println(“Sorted list of numbers”);
for (c = 0; c < n; c++) System.out.println(array[c]);
Once sorted, the program prints the sorted array by using a for loop.
Example Input and Output:
Input:
Input number of integers to sort
5
Enter 5 integers
64 25 12 22 11
Step-by-Step Sorting Process:
- First pass (c = 0):
Compare 64 and 25 → Swap → Array: [25, 64, 12, 22, 11]
Compare 64 and 12 → Swap → Array: [25, 12, 64, 22, 11]
Compare 64 and 22 → Swap → Array: [25, 12, 22, 64, 11]
Compare 64 and 11 → Swap → Array: [25, 12, 22, 11, 64]
- Second pass (c = 1):
Compare 25 and 12 → Swap → Array: [12, 25, 22, 11, 64]
Compare 25 and 22 → Swap → Array: [12, 22, 25, 11, 64]
Compare 25 and 11 → Swap → Array: [12, 22, 11, 25, 64]
- Third pass (c = 2):
Compare 12 and 22 → No swap → Array: [12, 22, 11, 25, 64]
Compare 22 and 11 → Swap → Array: [12, 11, 22, 25, 64]
- Fourth pass (c = 3):
Compare 12 and 11 → Swap → Array: [11, 12, 22, 25, 64]
The array is sorted now.
Output:
Sorted list of numbers
11
12
22
25
64
Conclusion:
- Bubble Sort: It is a simple sorting algorithm that compares adjacent elements repeatedly and swaps them if they are in the wrong order. This process is repeated for the entire array until it is sorted.
- Although it is simple, bubble sort is not efficient for large datasets because it has a time complexity of O(n^2), where n is the number of elements. It is slower than more advanced algorithms like Quick Sort or Merge Sort.