Amazon Affiliate

Tuesday 27 March 2012

WRITE A PROGRAM IN JAVA TO IMPLEMENT INSERTION SORT?

class insertionSort
{
        public static void main(String args[])
        {
              int a[]={2,6,4,8,9};
              int i, j, n;
               for (i = 1; i < a.length; i++)
              {
                      n = a[i];
                       j = i;
                      while (j > 0 && a[j - 1] > n)
                      {
                               a[j] = a[j - 1];
                                j--;
                       }
                      a[j] = n;
               }
               System.out.print("Sorted Array:");
              for(i=0;i<a.length;i++)
               System.out.print("\t" +a[i]);
          }
}

2 comments: