C Program Insertion Sort

#include<stdio.h>
#include<conio.h>
void main()
{
    clrscr();
    int size, array[50], i, j, temp;
    printf("Enter Array Size : ");
    scanf("%d",&size);
    printf("Enter Array Elements : ");
    for(i=0; i<size; i++)
    {
        scanf("%d",&array[i]);
    }
    printf("Sorting array using Insertion sort ... \n");
    for(i=1; i<size; i++)
    {
        temp=array[i];
        j=i-1;
        while((temp<array[j]) && (j>=0))
        {
            array[j+1]=array[j];
            j=j-1;
        }
        array[j+1]=temp;
    }
    printf("Array after sorting : \n");
    for(i=0; i<size; i++)
    {
        printf("%d  ", array[i]);
    }
    getch();
}

Run:


No comments:

Post a Comment

100 C Programs with Code and Output

Program 1: C Program To Read Two Numbers And Print The Sum Of Given Two Numbers. Program 2: C Program To Read Three Numbers And Prin...