Write a bubble sort program in java using for loop. The java programming language is used to create bubble sort. This is the basic concept behind bubble sorting, which is used to organise data in a specific order. The bubble sort method is also a significant method for beginners in programming languages. The simplest sorting algorithm is Bubble Sort, which works by repeatedly swapping adjacent elements if they are in the wrong order.

public class Main

{

static void sort(int[] arr)

{

int n = arr.length;

int temp = 0;

for(int i=0; i < n; i++)

{

for(int j=1; j < (n-i); j++)

{

if(arr[j-1] > arr[j])

{

temp = arr[j-1];

arr[j-1] = arr[j];

arr[j] = temp;

}

}

}

}

public static void main(String[] args)

{

int arr[] ={300,78,900,34,1,565,34,887,44,66};

System.out.println("Before sorting");

for(int i=0; i < arr.length; i++)

{

System.out.print(arr[i] + " ");

}

System.out.println();

sort(arr);

System.out.println("After sorting");

for(int i=0; i < arr.length; i++)

{

System.out.print(arr[i] + " ");

}

}

}

Output:

BeforeSorting

300 78 900 34 1 565 34 887 44 66

After sorting

1 34 34 44 66 78 300 565 887 900

## What is bubble sorting

To sort the array elements, Bubble Sort is one of the easiest sorting techniques in Java. The goal is to go from the first to the last element by comparing nearby items and exchanging them if they aren’t in the correct order.

## How to implement bubble sorting in java

Compare the current element to the next element of the array, starting with the first element (index = 0).

Swap the elements if the current element is greater than the array’s next element.

Move on to the next element if the current element is less than the next element. Step 1 should be repeated.

## How long will it take sort the bubble sort in java

In today’s world, a desktop computer can perform a billion (109) different tasks in less than 5 seconds.

A bubble sort on 106 random ints takes roughly 1012 small things, or 5000 seconds = 83 minutes.

In the enhanced version, the best case efficiency of bubble sort is O. (n).