Bubble Sort in Java – Learn How to Implement with Example!

Sorting is the general purpose process. We are doing knowingly or unknowing sorting in our daily life. Bubble Sort in Java is the one of the kind of sorting, in which two adjacents elements are compared. If you are not familiar with this type of sorting, don’t worry we will explain you with the help of example.

So, let’s start the journey.

What is Bubble Sort in Java?

Bubble sort algorithm is the simplest sorting algorithm, here the array is traversed from beginning to end. In other words, we compare adjacent elements. The first element is compared with the second one then the second is compared with the third one and so on. If the current element is greater than the next element then it is swapped.

Before we discuss the example, you should master the concept of Java Array

Java Bubble Sort Example

To sort an array of elements (7, 3 , 1 , 2 , 5), we are using Bubble Sort in Java.

First Pass:

7  3 1  2  5 ( The first two elements are compared and if the first is greater than second then we will swap)

=>  3  7  1  2  5 (no change because the first element is less than the second element)

7  1 2  5

=>  3  1  7  2  5 (swap because 7 is greater than 1)

1  7 2  5

=>  3  1  2  7  5 (swap because 7 is greater than 2)

3  1  2 7  5 (swap because 7 is greater than 5)

=>  3  1  2  5  7

Now the first pass is complete we will move towards the second pass

Second Pass:

3  1 2  5  7

=>  1  3  2  5  7 (swap because 3 is greater than 1 )

3  2 5  7

=>  1  2  3  5 7 (swap because 3 is greater than 2)

1  2 3  5 7 (no change)

1  2  3  5  7 (no change)

1  2  3  5  7 

Implementation of Bubble Sort in Java

The Bubble Sort algorithm will run itself once even if the array is sorted to run without any swap. The array is sorted now.

Get the Best & Interactive Way to Learn Java Quickly with the Infographic

Let us understand Bubble Sort with the help of the following example-

package com.dataflair.bubblesort;

public class BubbleSortProgram {
  void bubbleSort(int arrayDeclare[])
  {
    int arrayLength = arrayDeclare.length;
    for (int iterator1 = 0; iterator1 < arrayLength-1; iterator1++)
      for (int iterator2 = 0; iterator2 < arrayLength-iterator1-1; iterator2++)
        if (arrayDeclare[iterator2] > arrayDeclare[iterator2+1])
        {
          // swap temporaryVariable and arrayDeclare[iterator1]
          int temporaryVariable = arrayDeclare[iterator2];
          arrayDeclare[iterator2] = arrayDeclare[iterator2+1];
          arrayDeclare[iterator2+1] = temporaryVariable;
        }
  }
  /* Prints the array */
  void printArray(int arrayDeclare[])
  {
    int arrayLength = arrayDeclare.length;
    for (int iterator1=0; iterator1<arrayLength; ++iterator1)
      System.out.print(arrayDeclare[iterator1] + " ");
    System.out.println();
  }
  public static void main(String args[])
  {
    BubbleSortProgram ob = new BubbleSortProgram();
    int arrayDeclare[] = {72, 83, 69, 12, 9, 55, 18};
    ob.bubbleSort(arrayDeclare);
    System.out.println("Sorted Array is :");
    ob.printArray(arrayDeclare);
  }
}

The output of Bubble Sort in Java

Bubble-sort-program

Summary

As we said in introduction, Bubble sort in Java is the simplest type of sorting. Now, you can easily implement it in your code and save your time with this algorithm.

Hope, you liked the explanation. Please share your experience with us!

Check out the latest trends to build your career in Java

2 Responses

  1. Yashi says:

    Good explanation

    • DataFlair Team says:

      Hi Yashi,
      We are glad our reader liked our content. We recommend you to explore other Java tutorials as well, we have 100+ Java tutorials with a series of Java Interview Questions and quizzes. It will help you to brush up your skills,
      Regards,
      DataFlair

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.