class bubble_sort
{
public static void main(String args[])
{int a[]={2,5,4,6,7};
int i, j,t=0;
int n=a.length;
for(i = 0; i < n; i++)
{
for(j = 1; j < (n-i); j++)
{
if(a[j-1] > a[j])
{
t = a[j-1];
a[j-1]=a[j];
a[j]=t;
}
}
}
System.out.print("Sorted Array:");
for(i=0;i<a.length;i++)
System.out.print("\t" +a[i]);
}
}
{
public static void main(String args[])
{int a[]={2,5,4,6,7};
int i, j,t=0;
int n=a.length;
for(i = 0; i < n; i++)
{
for(j = 1; j < (n-i); j++)
{
if(a[j-1] > a[j])
{
t = a[j-1];
a[j-1]=a[j];
a[j]=t;
}
}
}
System.out.print("Sorted Array:");
for(i=0;i<a.length;i++)
System.out.print("\t" +a[i]);
}
}
No comments:
Post a Comment