# Bubble Sort Java Program

In this post we will learn bubble sort java program.

Bubble sort in program is considered as the simplest sorting algorithm for the fact that array is traversed from first number to last number.

It is considered as not so efficient sorting algorithm because it uses nested loops. Hence useful for small data sets.

Basically in the below program numbers are swapped if the first number is greater than the next number.

#### Example: Bubble Sort Java Program

Read Also – Insertion Sort In Java

```import java.util.Random;

public class Bubble_Sort {

static int[] sort(int[] in_Order){

for(int a = 0;a < in_Order.length;a++)

for(int b = 0;b < in_Order.length - 1;b++)

if(in_Order[b] > in_Order[b + 1]){

in_Order[b] = in_Order[b] + in_Order[b + 1];

in_Order[b + 1] = in_Order[b] - in_Order[b + 1];

in_Order[b] = in_Order[b] - in_Order[b + 1];
}
return in_Order;
}

static void print_In_Order(int[] proper_Order){

for(int x = 0;x < proper_Order.length;x++)

System.out.println(proper_Order[x] + " ");
}

public static void main(String[] args) {

System.out.println("Randomly sorting numbers using Bubble Sort ");

Random rd = new Random();

int r = 10;

int[] order = new int[r];

for(int x = 0;x < r;x++)

order[x] = Math.abs(rd.nextInt(1000));

System.out.println("Original Sequence: ");

print_In_Order(order);

System.out.println("Sorted Sequence: ");

print_In_Order(sort(order));
}
}```

Output: