Click here to hide categories Click here to show left categories

User: Home          welcome : Guest          Log In / Register here     




Insertion Sort

Insertion sort is a simple sorting algorithm, in which the sorted array (or list) is built and comparison of one entry at a time. It is much less efficient on large lists than more algorithms such as quicksort, heapsort, or merge sort. However, insertion sort provides several advantages: It have very simple implementation and efficient for small data sets

 /**************************************************************/
/*PROGRAM TO PERFORM INSERTION SORT IN C LANGUAGE*/
/**************************************************************/

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

#define INF -999

void main()
{
       int a[10],n,i,j,temp;

       clrscr();

       a[0]=INF;
       printf("Enter the size of array: ");
       scanf("%d",&n);
       printf("\nEnter the elements of array:\n");
       for(i=1;i<=n;i++)
       {      
              scanf("%d",&a[i]);
       }
       for(i=2;i<=n;i++)
       {
              temp=a[i];
              j=i-1;
              while(temp<a[j])
              {
                     a[j+1]=a[j];
                     j--;
              }
              a[j+1]=temp;
         }

       printf("\nAfter sorting the elements of array are:\n");
       for(i=1;i<=n;i++)
       {
              printf("%d ",a[i]);
       }
       getch();
}

Share this article   |    Print    |    Article read by 1271 times
Author:
Rohit kakria
I am software developer, moderator of xpode.com
Related Articles:
Related Interview Questions: No related interview question