C program for Insertion Sort Algorithm

C program for Insertion Sort Algorithm

229
2
SHARE

C program for Insertion Sort Algorithm :  Insertion sort is an example of an incremental algorithm. It builds the sorted sequence or list one number at a time. This is perhaps the simplest example of the incremental insertion technique, where we build up a complicated structure on n items by first building it on n − 1 items and then making the necessary changes to fix things in adding the last item. The given sequences are normally stored in arrays.  Let us see how to write a .

C program for Insertion Sort Algorithm  :

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

int main()
{
    int i,n, array[20], c, d, t;
	printf("-----------------------------------------------------------\n");
    printf("----------------------Made by C code champ-----------------\n");
    printf("-----------------------------------------------------------\n\n");
    printf("\t\tINSERTION SORT ALGORITHM\n\n\n\n");
	printf("Enter the number of input : ");
	scanf("%d",&n);
	printf("\n\nEnter the elements to be sorted :\n");
	for ( i = 0; i < n; i++)
	{
		 scanf("%d",&array[i]);
	}

  for (c = 1 ; c <= n - 1; c++) {
    d = c;

    while ( d > 0 && array[d] < array[d-1]) {
      t          = array[d];
      array[d]   = array[d-1];
      array[d-1] = t;

      d--;
    }
  } 
	printf("\n\nSorted List by Insertion Sort :\n");
	for ( i = 0; i < n; i++)
	{
		 printf("%d\t",array[i]);
	}
	getch();
    return 0;
}

We hope you all have enjoyed the of Insertion Sort Algorithm. If you have any queries or issues regarding the above code, ask us in form of comments..

2 COMMENTS

LEAVE A REPLY