Bubble Sort in Java – Definition & Examples

1. Java Bubble Sort – Objective

We closed our last chapter on Design Patterns in Java. Here, we are going to learn about the bubble sort in Java. First, we will see the meaning of Java Bubble Sort Algorithm. After that, we will understand Bubble Sort in Java with examples.

So, let us start Bubble Sort in Java.

Bubble Sort in Java

Bubble Sort in Java – Definition & Examples

2. What is Bubble Sort in Java?

Bubble Sort Algorithm in Java is understood as the simplest sorting algorithm. In the bubble sort algorithm, the array is traversed from initial element to the last element. Here, the current element is compared with the successive element. If the current element is bigger than the next element, it’s swapped.

The bubble is that the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they’re in wrong order. Now let’s understand Bubble Sort in Java with examples and coding.

Do you know the Working of Collections.Sort() in Java

3. Java Bubble Sort Example

First Pass:

( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, the algorithm compares the first two elements and swaps since 5 > 1.

( 1 5 4 2 8 ) –>  ( 1 4 5 2 8 ), Swap since 5 > 4

( 1 4 5 2 8 ) –>  ( 1 4 2 5 8 ), Swap since 5 > 2

( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), (8 > 5), algorithm does not swap them.

Second Pass:

( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )

( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2

( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )

( 1 2 4 5 8 ) –>  ( 1 2 4 5 8 )

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

Learn Java Array Tutorial – Creating, Initializing, and Accessing Array in Java

Third Pass:

( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )

( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )

( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )

( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )

class BubbleSort
{
   void bubbleSort(int arr[])
   {
       int n = arr.length;
       for (int i = 0; i < n-1; i++)
           for (int j = 0; j < n-i-1; j++)
               if (arr[j] > arr[j+1])
               {
                   // swap temp and arr[i]
                   int temp = arr[j];
                   arr[j] = arr[j+1];
                   arr[j+1] = temp;
               }
   }
   /* Prints the array */
   void printArray(int arr[])
   {
       int n = arr.length;
       for (int i=0; i<n; ++i)
           System.out.print(arr[i] + " ");
       System.out.println();
   }
   // Driver method to test above
   public static void main(String args[])
   {
       BubbleSort ob = new BubbleSort();
       int arr[] = {64, 34, 25, 12, 22, 11, 90};
       ob.bubbleSort(arr);
       System.out.println("Sorted array");
       ob.printArray(arr);
   }
}

Let’s learn Java URL Class With Examples

The output of Bubble Sorting in Java

Sorted array:

11 12 22 25 34 64 90

So, this was all about Bubble sort in Java. Hope you like our explanation.

4. Conclusion

Hence, in this Java Bubble sort tutorial, we studied the bubble sorting algorithm in Java. Moreover, we saw Bubble sort examples. Still, if any doubt regarding Bubble Sort in Java, ask in the comment tab.

See also –Java Virtual Machine (JVM)

For reference

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 *