Java program to print the bubble sort using for loop

Java program to print a right angle triangle pattern program using for loop. In the Java programming language, build a basic pattern program. To master any programming language, learn pattern programming. The pattern is printed in the first step of a complex level program written in a programming language. A decorative pattern that is replicated. In needlework and other crafts, a model or pattern is used as a reference.

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