Apply Bubble Sort on Data and Arrange in Ascending order and Descending order.
Program:
import java.util.Scanner;
class bubble{
public static void main(String arg[]){
Scanner sc=new Scanner(System.in);
int i;
int a[]=new int[5];
System.out.println("Enter numbers is array:");
for(i=0;i<5;i++)
{
a[i]=sc.nextInt();
}
bb b1=new bb();
b1.bubble_ace(a);
b1.bubble_dec(a);
}
}
class bb{
void bubble_ace(int a[])
{
int i,j,tmp;
for(i=0;i<5;i++)
{
for(j=0;j<5-1;j++)
{
if(a[j]>a[j+1])
{
tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
}
}
}
System.out.println("Accending order:");
for(i=0;i<5;i++)
System.out.println(a[i]);
}
void bubble_dec(int a[])
{
int i,j,tmp;
for(i=0;i<5;i++)
{
for(j=0;j<5-1;j++)
{
if(a[j]<a[j+1])
{
tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
}
}
}
System.out.println("Decending order:");
for(i=0;i<5;i++)
System.out.println(a[i]);
}
}
public static void main(String arg[]){
Scanner sc=new Scanner(System.in);
int i;
int a[]=new int[5];
System.out.println("Enter numbers is array:");
for(i=0;i<5;i++)
{
a[i]=sc.nextInt();
}
bb b1=new bb();
b1.bubble_ace(a);
b1.bubble_dec(a);
}
}
class bb{
void bubble_ace(int a[])
{
int i,j,tmp;
for(i=0;i<5;i++)
{
for(j=0;j<5-1;j++)
{
if(a[j]>a[j+1])
{
tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
}
}
}
System.out.println("Accending order:");
for(i=0;i<5;i++)
System.out.println(a[i]);
}
void bubble_dec(int a[])
{
int i,j,tmp;
for(i=0;i<5;i++)
{
for(j=0;j<5-1;j++)
{
if(a[j]<a[j+1])
{
tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
}
}
}
System.out.println("Decending order:");
for(i=0;i<5;i++)
System.out.println(a[i]);
}
}
No comments:
Post a Comment