Java Program to perform bubble sort

Here we are explaining how to write a java program to perform bubble sort. We make use of for loop to perform bubble sort an array of integers. Let's go through the following topics for a better understanding of this program,

What is bubble sort in java?

Bubble sort is a simple sorting algorithm. An array of numbers should be sorted in ascending order.

  • 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, swap if the first one is greater than 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

Bubble sort can also perform on Strings. We have a program to perform bubble sort on the string in our Java program collection. You can refer to that program too for additional pieces of information. 

How to implement a java program to perform bubble sort?

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 a 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 loop, we will get a sorted array and display the array.

by using nested for loop with the condition j<=limit-i-1, and j array[j+1], if true then change the position of array[j] to array[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 as array[i] using for loop.

 

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