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

1. 200 is compared with 55. Since 200 is not less than 55, swap of numbers will happen
2. 200 is compared with -13. Since 200 is not less than -13, swap of numbers will happen
3. 200 is compared with -7. Since 200 is not less than -7, swap of numbers will happen
4. 200 is compared with 75. Since 200 is not less than 75, swap of numbers will happen
5. 200 is compared with 100. Since 200 is not less than -100, swap of numbers will happen
6. 200 is compared with -22. Since 200 is not less than -22, swap of numbers will happen 2)Second Pass

1. 55 is compared with -13. Since 55 is not less than -13, swap of numbers will happen
2. 55 is compared with -7. Since 55 is not less than -7, swap of numbers will happen
3. 55 is compared with 75. Since 55 is less than 75,  No swap
4. 75 is compared with 100. Since 75 is less than 100, No swap
5. 100 is compared with -22. Since 100 is not less than -22, swap of numbers will happen 3)Third pass

1. -13 is compared with -7. Since -13 is  less than -7, No swap
2. -7 is compared with 55. Since -7 is  less than 55, No swap
3. 55 is compared with 75. Since 55 is  less than 75, No swap
4. 75 is compared with -22. Since 75 is  not less than -22, swap of numbers will happen 4)Forth pass

1. -13 is compared with -7. Since -13 is  less than -7, No swap
2. -7 is compared with 55. Since -7 is less than 55, No swap
3. 55 is compared with -22. Since 55 is  not less than -22, swap of numbers will happen 5)Fifth pass

1. -13 is compared with -7. Since -13 is  less than -7, No swap
2. -7 is compared with -22. Since -7 is  not less than -22, swap of numbers will happen 6)Sixth pass

1. -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
Array after Bubble Sort
-22 -13 -7 55 75 100 200