Thursday 12 December 2013

SORTING TECHNIQUES:-INSERTION SORTING

#include < stdio.h >
#include < conio.h > 

void main()
{
        int a[50],n;
        clrscr();
        printf("\nEnter n:");
        scanf("%d",&n);
        insert_sort(a,n);
        printf("\n%d values after sorting are ",n);
        print_data(a,n);
}

int insert_sort(int a[],int n)
{
        int bottom,i,j,t;
        bottom=-1;
        for(j=0;j < n;j++)
        {
                printf("\nData :");
                scanf("%d",&t);
                i=bottom;
                while(a[i]>t && i!=-1)
                {
                        a[i+1]=a[i];
                        i--;
                }
                a[i+1]=t;
                bottom++;
        }
        return;
}

int print_data(int a[],int n)
{
        int i;
        for(i=0;i  < n;i++)
        {
                printf("%4d",a[i]);
        }
        return;
}

No comments:

Post a Comment