# Bubble Sort Example Java Program

## Definition

Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps 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. It can be practical if the input is usually in sort order but may occasionally have some out-of-order elements nearly in position.

## Bubble Sort Example Program

```
import java.util.Scanner;
class BubbleSort {
public static void main(String []args) {
int num,swap;
Scanner in = new Scanner(System.in);
System.out.println("How many numbers are to be sorted:");
num = in.nextInt();
int array[] = new int[num];
System.out.println("Enter " + num + " integers");
for (int i = 0; i < num; i++){
array[i] = in.nextInt();
for (int i = 0; i < ( num - 1 ); i++) {
for (int j = 0; j < num - i - 1; j++) {
if (array[j] > array[j+1]){
swap       = array[j];
array[j]   = array[j+1];
array[j+1] = swap;
}
}
}
}
System.out.println("After sorting: ");
for (int i = 0; i < num; i++){
System.out.println(array[i]);
}
}
}
```

## Sample Output

```Output is:
How many numbers are to be sorted:
7
Enter 7 integers
34
67
32
78
945
1111
2
After sorting:
2
32
34
67
78
945
1111```