Friday, April 15, 2011

Bubble Sort 2


import java.util.Scanner;
import java.util.Timer;
class BubbleSort2
{
    static void Bubble(int x[],int n)
    {
        int i,j,t;
        boolean switched=true;
        for(i=0;(i<n-1)&&(switched==true);i++)
        {
            switched=false;
            for(j=0;j<n-i-1;j++)
                if(x[j]>x[j+1])
                {
                    switched=true;
                    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] + "  ");
    }
}