# Bubble sort in java

What you will learn here about Bubble Sort

- What is Bubble Sort
- How Bubble Sort works
- Implementation of Bubble sort in java
- Time Complexity of Bubble Sort

## What is Bubble Sort

The Bubble sort is the sorting algorithm which is used to sort the elements in the order.

## How Bubble Sort works

The bubble sort algorithm repeatedly steps through the list of elements, compares adjacent elements and swaps them if they are in the wrong order. The one cycle of comparing adjacent elements starting from first element to last element is called as pass. The list elements are repeated through several passes to get sorted elements list.

## Example of Bubble Sort

1)First pass

- 200 is compared with 55. Since 200 is not less than 55, swap of numbers will happen
- 200 is compared with -13. Since 200 is not less than -13, swap of numbers will happen
- 200 is compared with -7. Since 200 is not less than -7, swap of numbers will happen
- 200 is compared with 75. Since 200 is not less than 75, swap of numbers will happen
- 200 is compared with 100. Since 200 is not less than -100, swap of numbers will happen
- 200 is compared with -22. Since 200 is not less than -22, swap of numbers will happen

2)Second Pass

- 55 is compared with -13. Since 55 is not less than -13, swap of numbers will happen
- 55 is compared with -7. Since 55 is not less than -7, swap of numbers will happen
- 55 is compared with 75. Since 55 is less than 75, No swap
- 75 is compared with 100. Since 75 is less than 100, No swap
- 100 is compared with -22. Since 100 is not less than -22, swap of numbers will happen

3)Third pass

- -13 is compared with -7. Since -13 is less than -7, No swap
- -7 is compared with 55. Since -7 is less than 55, No swap
- 55 is compared with 75. Since 55 is less than 75, No swap
- 75 is compared with -22. Since 75 is not less than -22, swap of numbers will happen

4)Forth pass

- -13 is compared with -7. Since -13 is less than -7, No swap
- -7 is compared with 55. Since -7 is less than 55, No swap
- 55 is compared with -22. Since 55 is not less than -22, swap of numbers will happen

5)Fifth pass

- -13 is compared with -7. Since -13 is less than -7, No swap
- -7 is compared with -22. Since -7 is not less than -22, swap of numbers will happen

6)Sixth pass

- -13 is compared with -22. Since -13 is not less than -22, swap of numbers will happen

## Implementation of Bubble sort in java

public class BubbleSort { public static void main(String[] args) { int[] numArray = { 2, 55, -13, -7, 75, 100, 22 }; System.out.println("Array before Bubble Sort"); for (int i = 0; i < numArray.length; i++) { System.out.print(numArray[i] +" "); } for (int index = (numArray.length - 1); index > 0;index--) { for (int i = 0; i < index; i++) { if (numArray[i] > numArray[i + 1]) { int temp = numArray[i]; numArray[i] = numArray[i+1]; numArray[i+1] = temp; } } } System.out.println("\n\nArray after Bubble Sort"); for (int i = 0; i < numArray.length; i++) { System.out.print(numArray[i] +" "); } } }

### Bubble Sort Output

Array before Bubble Sort

200 55 -13 -7 75 100 -22 21

200 55 -13 -7 75 100 -22 21

Array after Bubble Sort

-22 -13 -7 55 75 100 200

-22 -13 -7 55 75 100 200