Java Program to Sort an Array Using Bubble Sort

Introduction

Bubble Sort is one of the simplest sorting algorithms that sorts an array by repeatedly stepping through the list, comparing adjacent elements, and swapping them if they are in the wrong order. The process is repeated until the array is sorted. This guide will walk you through writing a Java program that sorts an array using the Bubble Sort algorithm.

Problem Statement

Create a Java program that:

  • Prompts the user to enter the size of an array and its elements.
  • Sorts the array using the Bubble Sort algorithm.
  • Displays the sorted array.

Example:

  • Input: [5, 1, 4, 2, 8]
  • Output: "Sorted array: [1, 2, 4, 5, 8]"

Solution Steps

  1. Read the Array Size and Elements: Use the Scanner class to take the size and elements of the array as input from the user.
  2. Implement Bubble Sort: Use nested loops to repeatedly swap adjacent elements if they are in the wrong order.
  3. Display the Sorted Array: Print the sorted array.

Java Program

// Java Program to Sort an Array Using Bubble Sort
// Author: https://www.rameshfadatare.com/

import java.util.Scanner;

public class BubbleSort {
    public static void main(String[] args) {
        // Step 1: Read the size and elements of the array from the user
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("Enter the size of the array: ");
        int size = scanner.nextInt();
        
        int[] array = new int[size];
        
        System.out.println("Enter the elements of the array:");
        for (int i = 0; i < size; i++) {
            array[i] = scanner.nextInt();
        }
        
        // Step 2: Sort the array using Bubble Sort
        bubbleSort(array);
        
        // Step 3: Display the sorted array
        System.out.println("Sorted array:");
        for (int i = 0; i < size; i++) {
            System.out.print(array[i] + " ");
        }
    }
    
    // Method to implement Bubble Sort
    public static void bubbleSort(int[] array) {
        int n = array.length;
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (array[j] > array[j + 1]) {
                    // Swap array[j] and array[j + 1]
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }
    }
}

Explanation

Step 1: Read the Array Size and Elements

  • The Scanner class is used to read the size of the array and its elements. The nextInt() method captures the size and each element.

Step 2: Implement Bubble Sort

  • Outer Loop: The outer loop runs n-1 times, where n is the size of the array.
  • Inner Loop: The inner loop runs from the start of the array to n-i-1, where i is the current iteration of the outer loop. It compares each pair of adjacent elements and swaps them if they are in the wrong order (i.e., if the first element is greater than the second).
  • Swapping: If array[j] > array[j + 1], the elements are swapped using a temporary variable.

Step 3: Display the Sorted Array

  • The sorted array is printed using a for loop.

Output Example

Example 1:

Enter the size of the array: 5
Enter the elements of the array:
5 1 4 2 8
Sorted array:
1 2 4 5 8

Example 2:

Enter the size of the array: 6
Enter the elements of the array:
64 34 25 12 22 11
Sorted array:
11 12 22 25 34 64

Conclusion

This Java program demonstrates how to sort an array using the Bubble Sort algorithm. It covers essential concepts such as nested loops, array manipulation, and basic sorting techniques, making it a valuable exercise for beginners learning Java programming.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top