Sorting an Array in ascending order using Bubble Sort Algo. in Java. Remove the comment section to see the value of j in each iteration. 

class bsort
{
public static void main(String args[])
{
int max=5; 
int ar[]={9,3,5,1,2};
int i,j,temp;


for(i=1;i<max;i++)
{
for(j=max-1;j>=i;j--)
{
if(ar[j-1]>ar[j])

{
temp=ar[j-1];

ar[j-1]=ar[j];

ar[j]=temp;
//System.out.println("j= "+j);
}


}

}

System.out.println("Sorted Array : ");
for(i=0;i<max;i++)
{

System.out.println(ar[i]);
System.out.println(" ");
}

}

}

0 comments:

Post a Comment

 
Top