Friday, January 21, 2011

BubleSort Using Two Variables

class BubbleSort
{
    void sort(int a[])
    {
        for(int i=0;i<a.length;i++)
        {
            for(int j=i;j<a.length;j++)
            {
                if(a[i]>a[j])
                {
                    a[i]=a[i]+a[j];
                    a[j]=a[i]-a[j];
                    a[i]=a[i]-a[j];
                }
            }
        }

        System.out.print("Sorted Numbers : ");
       
        for(int i=0;i<a.length;i++)
            System.out.print("\t"+a[i]);
       
        System.out.println("\n");
    }

}


/**
 *
 * @author Sanket
 */
public class Main {
    public static void main(String[] args)throws IOException {
        BubbleSort b=new BubbleSort();

        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Entrer total numbers : ");
        int n=Integer.parseInt(br.readLine());

        int a[]=new int[n];
        for(int i=0;i<n;i++)
        {
            System.out.print("Enter number "+(i+1)+" : ");
            a[i]=Integer.parseInt(br.readLine());
        }
        b.sort(a);
    }
}

No comments:

Post a Comment