import java.util.*;
public class InsertionSort {
    private int x[],n;
    public InsertionSort(int n)
    {
        this.n=n;
        x=new int[n];
        Scanner src=new Scanner(System.in);
        for(int i=0;i<n;i++)
            x[i]=src.nextInt();
    }
    public void display()
    {
        System.out.println("The sorted elements are: ");
        for(int i=0;i<n;i++)
            System.out.print(x[i]+" ");
        System.out.println();
    }
    public void insertionSort()
    {
        for(int i=0;i<n-1;i++)
        {
            int j;
            int val=x[i+1];
            for(j=i;j>=0;j--)
                if(x[j]>val)
                    x[j+1]=x[j];
                else break;
            x[j+1]=val;
        }
    }

}
class InsertionSortExp
{
    public static void main(String args[])
    {
            Scanner src=new Scanner(System.in);
            System.out.println("Enter the no of elements");
            int n=src.nextInt();
            System.out.println();
            InsertionSort obj=new InsertionSort(n);
            obj.insertionSort();
            obj.display();

        }
    }

O/P:

Enter the no of elements
10

11
78
45
31
22
99
84
52
4
19
The sorted elements are: 
4 11 19 22 31 45 52 78 84 99 
BUILD SUCCESSFUL (total time: 18 seconds)
