Bubble Sort - Java
import java.util.*;
class Bubsort
{
int a[]=new int[50];
int i,n,j;
Scanner s=new Scanner(System.in);
void read()
{
System.out.println("Enter number of elements...");
n=s.nextInt();
System.out.println("Enter Elements.....");
for(i=0;i<n;i++)
a[i]=s.nextInt();
}
void sort()
{
boolean excg=true;
for(i=0;i<n-1&&excg;i++)
{
excg=false;
for(j=0;j<n-1-i;j++)
if(a[j]>a[j+1]){excg=true;int t=a[j];a[j]=a[j+1];a[j+1]=t;}
}
}
void disp()
{
System.out.println("Sorted Array is..............");
for(i=0;i<n;i++)
System.out.println(a[i]);
}
}
class Main
{
public static void main(String args[])
{
Bubsort bs=new Bubsort();
bs.read();
bs.sort();
bs.disp();
}
}
Comments
Post a Comment