Back to: Java Tutorials

Bubble sort is a simple sorting algorithm that repeatedly steps through the list of elements to be sorted, compares each pair of adjacent elements, and swaps them if they are in the wrong order. This process is repeated until no more swaps are needed, indicating that the list has been sorted.

#### Bubble Sort Explanation

To understand bubble sort, consider the following list of numbers: [5, 3, 8, 4, 2]. The algorithm works by comparing adjacent elements in the list and swapping them if they are out of order. In the first pass of the algorithm, the first two elements are compared and swapped if necessary. The next pair of elements are then compared and swapped if necessary, and so on, until the end of the list is reached. After the first pass, the largest element in the list is in its correct position at the end of the list.

The figure below shows the first pass of the bubble sort algorithm on the list [5, 3, 8, 4, 2]:

```
Pass 1:
[5, 3, 8, 4, 2] -> [3, 5, 8, 4, 2]
^
[3, 5, 8, 4, 2] -> [3, 5, 4, 8, 2]
^
[3, 5, 4, 8, 2] -> [3, 5, 4, 2, 8]
^
The largest element, 8, is now in its correct position at the end of the list.
```

In the second pass, the algorithm compares and swaps the first two elements, then the next two elements, and so on, until the second largest element is in its correct position at the end of the list. The figure below shows the second pass of the bubble sort algorithm on the list [3, 5, 4, 2, 8]:

```
Pass 2:
[3, 5, 4, 2, 8] -> [3, 4, 5, 2, 8]
^
[3, 4, 5, 2, 8] -> [3, 4, 2, 5, 8]
^
The second largest element, 5, is now in its correct position before the largest element, 8.
```

The algorithm continues with subsequent passes until the list is completely sorted in ascending order. The figure below shows the complete bubble sort algorithm on the list [5, 3, 8, 4, 2]:

```
Pass 1:
[5, 3, 8, 4, 2] -> [3, 5, 8, 4, 2]
^
[3, 5, 8, 4, 2] -> [3, 5, 4, 8, 2]
^
[3, 5, 4, 8, 2] -> [3, 5, 4, 2, 8]
^
Pass 2:
[3, 4, 5, 2, 8] -> [3, 4, 2, 5, 8]
^
Pass 3:
[3, 4, 2, 5, 8] -> [3, 2, 4, 5, 8]
^
Pass 4:
[2, 3, 4, 5, 8] -> [2, 3, 4, 5, 8]
^
```

#### Java Code Example on Bubble Sort

Here’s an example of how to implement bubble sort in Java:

```
public class BubbleSort {
public static void main(String[] args) {
int[] arr = {10, 2, 6, 8, 3};
int n = arr.length;
// loop through the array
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
// compare adjacent elements
if (arr[j] > arr[j+1]) {
// swap them if they are in the wrong order
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
// print the sorted array
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
}
}
```

In this code, we first define an array of integers to be sorted. Then, we loop through the array and compare each adjacent pair of elements. If they are in the wrong order, we swap them. We repeat this process until no more swaps are needed. Finally, we print out the sorted array. Note that bubble sort is not the most efficient sorting algorithm for large lists, but it can be useful for smaller lists or for educational purposes.

**Input: **The input to this program is an integer array named `arr`

which contains the values {10, 2, 6, 8, 3}.

**Output:** The output of this program is the sorted array printed to the console, which will be {2, 3, 6, 8, 10} in this case. The `System.out.print()`

method is used to print each element of the sorted array on the same line, separated by a space.

The output of this program will be:

`2 3 6 8 10 `

Also, see the example code JavaExamples_NoteArena in our GitHub repository. See complete examples in our GitHub repositories.

Follow us on social media

Follow Author