F How to implement Insertion Sort in C program ? | CodeTheta

How to implement Insertion Sort in C program ?

May 18, 2013

This is a simple Insertion sorting programming and it is written in C programming. This insertion sort implementing using static array , you can see the array size is 100 which is written in the program. at first you have to give some element when system ask you to enter the element then system will calculate those element and display the sorted array element.
In array you can add , modify , delete the element from the array also.

/*
  Program to implement INSERTION SORTING in C.
  --------------------------------------------
*/

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

void insert_sort(int [],int);

void main()
{
int i,n,A[100];
clrscr();
printf("\n Enter the size of array : ");
scanf("%d",&n);
printf("\n Enter the elements -> \n");
for(i=1;i<=n;i++)
   scanf("%d",&A[i]);
insert_sort(A,n);
printf("\n\n The Sorted Array is -> ");
for(i=1;i<=n;i++)
   printf(" %d ",A[i]);
getch();
}

//INSERTION SORT.
void insert_sort(int l[], int n)
 {
 int ptr,temp;
 int i,k;
 l[0]=0;
 for(i=1;i<=n;i++)
    {
    ptr=i-1;
    temp=l[i];
    while(temp<l[ptr])
  {
  l[ptr+1]=l[ptr];
  ptr--;
  }
    l[ptr+1]=temp;
    printf("Step = %d",i);
    for(k=0;k<=n;k++)
       printf(" %d",l[k]);
    printf("\n");
    }
 }
Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us

Post a Comment