Bubble Sort in Java

In this post, we will learn how to implement Bubble sort in Java.
Sorting Algorithm in java.

Bubble Sort Algorithm

Bubble sorting is the simplest sorting algorithm that works by comparing two adjacent elements in an array and swapping them if found in the wrong order.

Bubble sort compares the first element with the next one and if found in the wrong order then that compared element in an array are swapped. This algorithm traverse through the entire element in an array.

Time Complexity of Bubble Sort:

Best case:  O(n)
Average case:  O(n^2)
Worst case:  O(n^2)

Bubble sort in Java program:

We will see the two examples on Bubble sorting in java, one defining the elements in program and another by taking user input.

1. Defining the elements in a program.

The Output of Bubble Sort in Java.

2. By User-Input.

The Output of the Bubble sorting algorithm in java.