/*

 * main.cpp

 *

 *  Created on: Jul 29, 2010

 *      Author: administrator

 */

#include <iostream>

using namespace std;

#define ELEMENTS 6

void printlist(int list[],int n)

{

   int i;

   for(i=0;i<n;i++)

      printf("%d\t",list[i]);

}

//Method II with Insert Function built into Sorting

void insertion_sort(int x[],int length)

{

  int key;

  for(int i=1;i<length;i++)

  {

    key=x[i];

    int j;

    for (j = i-1; j>= 0; j--)

    {

            if (key < x[j])

                    x[j+1] = x[j];

            else break;

    }

    x[j+1] = key;

    printlist(x,length);

    printf("%s%d\n", "After insertion of element " , i);

  }

}

int main()

{

  int A[ELEMENTS]={5,2,4,6,1,3};

  cout<<"NON SORTED LIST:"<<endl;

  printlist(A, ELEMENTS);

  printf("\n");

  insertion_sort(A,ELEMENTS);

  cout<<endl<<"SORTED LIST"<<endl;

  printlist(A, ELEMENTS);

  return 0;

}