Insertion Sort in C.( C program for Insertion Sorting ).

#include<stdio.h>
int main()
{
    int a[50],i,j,n,temp;
    printf("Enter Array size: ");
    scanf("%d",&n);
    printf("Enter %d elements in Array: \n",n);
    for(i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=1; i<n; i++)
    {
        temp=a[i];
        j=i-1;
        while(j>=0 && a[j]>temp)
        {
            a[j+1]=a[j];
            j=j-1;
        }
        a[j+1]=temp;
    }
    printf("Your Entered Array is:\n");
    for(i=0; i<n; i++)
    {
        printf("\n\t%d",a[i]);
    }
    return 0;
}


*** Input && Output ***



Post a Comment

0 Comments