Bubble Sort in Java
package code.example.basic; public class BubbleSortExample { public static void main(String args[]){ int ar[]={4,5,6,7,3,4,2}; for(int i=0;i<ar.length;i++){ for(int j=0;j<(ar.length-1)-i;j++){ if(ar[j]>ar[j+1]){ ar[j]= ar[j]+ar[j+1]-(ar[j+1]=ar[j]); } } for(int k=0;k<ar.length;k++){ System.out.print(ar[k]);//this print only understanding } System.out.println("-");//this print only understanding } for(int i=0;i<ar.length;i++){ System.out.println(ar[i]); } } }
(ar.length-1)-i -- This code used for optimization
post a comment