Java program to print the bubble sort using for loop

Java program to print the bubble sort 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