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(" ");
}

}

}
13 Nov 2013

0 comments:

Post a Comment

:) :)) ;(( :-) =)) ;( ;-( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ $-) (b) (f) x-) (k) (h) (c) cheer
Click to see the code!
To insert emoticon you must added at least one space before the code.

 
Top