`Bubble sort`

, sometimes incorrectly referred to as `sinking sort`

, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order.

The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm gets its name from the way smaller elements `bubble`

to the top of the list.

Because it only uses comparisons to operate on elements, it is a comparison sort. Although the algorithm is simple, most of the other sorting algorithms are more efficient for large lists.

### Logic is Simple:

In bubble sort, we basically traverse the arraylist from first to (size – 1) position and compare the element with the next one. Swap Element with the next element only if the next element is greater.

### Java Code:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
package crunchify.com.tutorials; import java.util.Arrays; /** * @author Crunchify.com. * How to Implement Bubble sort algorithm in Java? Ascending and Descending Order Tutorial */ public class CrunchifyBubbleSort { public static void main(String[] args) { int crunchifyArry[] = { 15, 3, 9, 7, 19, 8, 1, 5 }; log("Let's get started on Bubble Sort implementation in Java \n"); log("============ Ascending Order result:" + Arrays.toString(CrunchifyBubbleSortAsceMethod(crunchifyArry)) + "\n"); log("============ Descending Order result:" + Arrays.toString(CrunchifyBubbleSortDescMethod(crunchifyArry)) + "\n"); } // Bubble Sort Algorithm in Ascending Order public static int[] CrunchifyBubbleSortAsceMethod(int[] crunchifyArr) { int temp; for (int i = 0; i < crunchifyArr.length - 1; i++) { for (int j = 1; j < crunchifyArr.length - i; j++) { if (crunchifyArr[j - 1] > crunchifyArr[j]) { temp = crunchifyArr[j - 1]; crunchifyArr[j - 1] = crunchifyArr[j]; crunchifyArr[j] = temp; } } log("Iteration " + (i + 1) + ": " + Arrays.toString(crunchifyArr)); } return crunchifyArr; } // Bubble Sort Algorithm in Descending Order public static int[] CrunchifyBubbleSortDescMethod(int[] crunchifyArr) { int temp; for (int i = 0; i < crunchifyArr.length - 1; i++) { for (int j = 1; j < crunchifyArr.length - i; j++) { if (crunchifyArr[j - 1] < crunchifyArr[j]) { temp = crunchifyArr[j - 1]; crunchifyArr[j - 1] = crunchifyArr[j]; crunchifyArr[j] = temp; } } log("Iteration " + (i + 1) + ": " + Arrays.toString(crunchifyArr)); } return crunchifyArr; } // Simple log util private static void log(String result) { System.out.println(result); } } |

### Eclipse Console Result

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Let's get started on Bubble Sort implementation in Java Iteration 1: [3, 9, 7, 15, 8, 1, 5, 19] Iteration 2: [3, 7, 9, 8, 1, 5, 15, 19] Iteration 3: [3, 7, 8, 1, 5, 9, 15, 19] Iteration 4: [3, 7, 1, 5, 8, 9, 15, 19] Iteration 5: [3, 1, 5, 7, 8, 9, 15, 19] Iteration 6: [1, 3, 5, 7, 8, 9, 15, 19] Iteration 7: [1, 3, 5, 7, 8, 9, 15, 19] ============ Ascending Order result:[1, 3, 5, 7, 8, 9, 15, 19] Iteration 1: [3, 5, 7, 8, 9, 15, 19, 1] Iteration 2: [5, 7, 8, 9, 15, 19, 3, 1] Iteration 3: [7, 8, 9, 15, 19, 5, 3, 1] Iteration 4: [8, 9, 15, 19, 7, 5, 3, 1] Iteration 5: [9, 15, 19, 8, 7, 5, 3, 1] Iteration 6: [15, 19, 9, 8, 7, 5, 3, 1] Iteration 7: [19, 15, 9, 8, 7, 5, 3, 1] ============ Descending Order result:[19, 15, 9, 8, 7, 5, 3, 1] |

### What is a Time Complexity of Bubble Sort Algorithm?

- If you consider
`Best case`

scenario then it would be`O(n)`

- If you consider
`Worst case`

scenario then it would be`O(n`

^{2})