Friday, April 15, 2011

Insertion Sort


import java.util.Scanner;
class InsertsertionSort
{
    static void Insertion(int x[],int n)
    {
        int i,k,y;
        for(k=1;k<n;k++)
        {
            y=x[k];
            for(i=k-1;i>=0&&y<x[i];i--)
                x[i+1]=x[i];
            x[i+1]=y;
        }
    }
   
    public static void main(String args[ ])
    {
                int i,n=6;
        Scanner in = new Scanner(System.in);
        int x[]=new int[n];
        System.out.println("Enter numbers");
        for(i=0;i<n;i++)
            x[i] = in.nextInt();
        Insertion(x,n);

        System.out.println("\nSorted Elements are :");
        for(i=0;i<n;i++)
        System.out.print(x[i] + "  ");
    }
}