Friday, January 13, 2012

Sorting Algorithm :: Insertion sort

#include<stdlib.h>

int main()
{
    int a[23],n,j,i,t=0,k;
    printf("enter the n\n");
    while(scanf("%d",&n)==1)
    {
        printf("enter the array\n");
        a[0]=-455555;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            for(j=2;j<=i;j++)
            {
                t=a[j]; k=j-1;
                while(t<a[k])
                {
                    a[k+1]=a[k]; k--;
                }
                a[k+1]=t;
            }
        }
        printf("print the sorted array\n");
        for(i=1;i<=n;i++)
            printf("%d\t",a[i]);
    }
    return 0;
}

No comments:

Post a Comment