Java Program to perform bubble sort


January 9, 2023, Learn eTutorial
1543

What is bubble sort?

Bubble sort is a simple sorting algorithm. Suppose we need an array of numbers should be sorted in ascending order. we have to check the steps below

  • Check the first two adjacent numbers
  • If the first number is greater than the second number then swap those numbers
  • Next check the 2nd and 3rd numbers, and swap if the first one is greater than the second.
  • Repeat this process till the last numbers are checked,

So after the first iteration, the biggest number is placed at its proper position ie., at the last index of an array. Do the same process until the numbers are sorted completely.

 Java - Perform bubble sort

How to perform bubble sort using a Java program?

First, we have to declare the class 'BubbleSort'. Declare the integer variables 'i, j, temp, limit'. Create an object of the scanner class as sc. Read the limit of the array elements as a 'limit'. Declare an integer array of size 'limit'. Read the array elements using an for loop into 'array[ ]'. Then by using nested for loop compare the adjacent elements, if the first element is greater than the second element then swap the elements. After the execution of the inner for loop, we get one element in the right position. Now repeat again the outer for loot until we will get a sorted array, then display the result.

by using nested for loop with the condition 'j<=limit-i-1', and 'j arr[j+1]', if true then change the position of 'arr[j] to arr[j+1]' and repeat the loop.

ALGORITHM

STEP 1: Declare the class BubbleSort with a public modifier.

STEP 2: Open the main() to start the program, and Declare the integer variables i, j, temp, limit

STEP 3: Read the limit of the array into the variable limit.

STEP 4: Declare an array of size limit.

STEP 5:Read the elements into the 'array' by using for loop from i = 0 to limit.

STEP 6: Using a nested for loop traverse the array and check if adjacent elements 'array[j] > array[j+1]' , if it is true then swap both elements.  

STEP 7: Display the sorted list using for loop.


Bubble sort algorithm implementation uses the below concepts of java programming, please refer to these topics for a better understanding


Bubble sort can also perform on Strings. please refer to the program to perform bubble sort on the string in our Java program collection.  

Java Source Code

                                          import java.util.Scanner;

public class BubbleSort {
  public static void main(String []args) {
    int i,j,temp,limit;
    Scanner sc = new Scanner(System.in);
 
    System.out.println("Enter the limit of the numbers:");
    limit = sc.nextInt();
 
    int array[] = new int[limit];
 
    System.out.println("Enter " + limit + " numbers: ");
    for (i = 0; i <limit; i++) 
      array[i] = sc.nextInt();
 
    for (i = 0; i < ( limit - 1 ); i++) {
      for (j = 0; j < limit - i - 1; j++) {
        if (array[j] > array[j+1]) //swap the elements if first one is greater than second
        {
           temp = array[j];
           array[j] = array[j+1];
           array[j+1] = temp;
        }
      }
    }
 
    System.out.println("******Sorted list******");
    for (i = 0; i < limit; i++) 
      System.out.println(array[i]);
  }
}
                                      

OUTPUT

Enter the limit of the numbers:5
Enter 5 numbers: 
9
10
23
7
4
******Sorted list******
4
7
9
10
23