import java.util.Scanner;
class BubbleSort1
{
static void Bubble(int x[],int n)
{
int t,i,j;
for(i=0;i<n-1;i++)
for(j=0;j<n-1;j++)
if(x[j]>x[j+1]) // Change to < for Descending Order
{
t=x[j];
x[j]=x[j+1];
x[j+1]=t;
}
}
public static void main(String args[ ])
{
int i,n=10;
Scanner in = new Scanner(System.in);
System.out.print("Enter how many numbers to be sorted : ");
n = in.nextInt();
int x[]=new int[n];
System.out.println("Enter numbers");
for(i=0;i<n;i++)
x[i] = in.nextInt();
Bubble(x,n);
System.out.println("\nSorted Elements are :");
for(i=0;i<n;i++)
System.out.print(x[i] + " ");
}
}
No comments:
Post a Comment