HOME

Tuesday, April 18, 2017

INSERTION SORT- C PROGRAM

#include<stdio.h>
#include<conio.h>
void main()
{
    int i,j,n,a[20],temp;
    printf("Enter size of array\n");
    scanf("%d",&n);
    printf("\nInput %d value\n",n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=0;i<n;i++)
    {
        j=i;
        while(j>0 && a[j-1]>a[j])
        {
            temp=a[j];
            a[j]=a[j-1];
            a[j-1]=temp;
            j--;
        }
    }
    printf("\nValue after sorted are:\n");
    for(i=0;i<n;i++)
    {
        printf("%d\n",a[i]);

    }
getch();
}

No comments:

Post a Comment