Insertion sort in C program

Here is the program to sort the given integer in ascending order using insertion sort method. Please find the pictorial tutor of the insertion sorting.

Logic : Here, sorting takes place by inserting a particular element at the appropriate position, that’s why the name-  insertion sorting. In the First iteration, second element A[1] is compared with the first element A[0]. In the second iteration third element is compared with first and second element. In general, in every iteration an element is compared with all the elements before it. While comparing if it is found that the element can be inserted at a suitable position, then space is created for it by shifting the other elements one position up and inserts the desired element at the suitable position. This procedure is repeated for all the elements in the list.

If we complement the if condition in this program, it will give out the sorted array in descending order. Sorting can also be done in other methods, like selection sorting and bubble sorting, which follows in the next pages.

Insertion Sort Demo
Insertion Sort Demo

C program for Insertion Sort:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include
void main()
{
	int A[20], N, Temp, i, j;
	clrscr();
	printf("\n\n\t ENTER THE NUMBER OF TERMS...: ");
	scanf("%d", &N);
	printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:");
	for(i=0; i<N; i++)
	{
		scanf("\n\t\t%d", &A[i]);
	}
	for(i=1; i<N; i++)
	{
		Temp = A[i];
		j = i-1;
		while(Temp<A[j] && j>=0)
		{
			A[j+1] = A[j];
			j = j-1;
		}
		A[j+1] = Temp;
	}
	printf("\n\tTHE ASCENDING ORDER LIST IS...:\n");
	for(i=0; i<N; i++)
		printf("\n\t\t\t%d", A[i]);
	getch();
}

Download exe and source code here.

194 Responses to “Insertion sort in C program”

    • Source code of simple merge sort implementation using array in ascending order in c programming language

      #include
      #define MAX 50

      void mergeSort(int arr[],int low,int mid,int high);
      void partition(int arr[],int low,int high);

      int main(){

      int merge[MAX],i,n;

      printf(“Enter the total number of elements: “);
      scanf(“%d”,&n);

      printf(“Enter the elements which to be sort: “);
      for(i=0;i<n;i++){
      scanf("%d",&merge[i]);
      }

      partition(merge,0,n-1);

      printf("After merge sorting elements are: ");
      for(i=0;i<n;i++){
      printf("%d ",merge[i]);
      }

      return 0;
      }

      void partition(int arr[],int low,int high){

      int mid;

      if(low<high){
      mid=(low+high)/2;
      partition(arr,low,mid);
      partition(arr,mid+1,high);
      mergeSort(arr,low,mid,high);
      }
      }

      void mergeSort(int arr[],int low,int mid,int high){

      int i,m,k,l,temp[MAX];

      l=low;
      i=low;
      m=mid+1;

      while((l<=mid)&&(m<=high)){

      if(arr[l]mid){
      for(k=m;k<=high;k++){
      temp[i]=arr[k];
      i++;
      }
      }
      else{
      for(k=l;k<=mid;k++){
      temp[i]=arr[k];
      i++;
      }
      }

      for(k=low;k<=high;k++){
      arr[k]=temp[k];
      }
      }

      Sample output:

      Enter the total number of elements: 5
      Enter the elements which to be sort: 2 5 0 9 1
      After merge sorting elements are: 0 1 2 5 9

      Reply
  1. chetna goyal

    hii..i want to know the meaning of why we use j=j-1 after assigning temp to a[i] in insertion sort..please explain it as soon as possible my mail id is [email protected].

    Reply
  2. karamjeet virk

    main()
    #define size 10
    {
    int arr[size],i,j,item;
    printf(“\n enter the element in array”);
    for(i=0;i<size;i++)
    scanf("%d",&arr[i]);
    for(j=0;j<size;j++)
    { item=arr[i];
    for(i=j-1;item=0;i–)
    { arr[i+1]=arr[i];
    arr[i]=item;
    }}

    Reply
  3. For balchhal programs contact AOT guys….West bengal…chodao bara

    Reply
  4. pls multy way tree sample program for ds insertion deletion plz............

    multy way tree

    Reply
  5. ATCHUTA RAO

    #include
    #include
    void main()
    {
    int a[20],k,n,i=0,j=0,t=0;

    clrscr();
    printf(“no of of elemts”);
    scanf(“%d”,&n);
    printf(“enter %d values”,n);
    for(i=0;i<n;i++)
    scanf("%d",&a[i]);

    for(i=1;i=1;j–)
    {

    printf(“i=%d j=%d %d %d”,i,j,a[j],a[j-1]);
    if(a[j]>a[j-1])
    break;
    else
    {
    t=a[j];
    a[j]=a[j-1];
    a[j-1]=t;
    }

    } printf(“\n”);
    } printf(“\n”);

    for(i=0;i<n;i++)
    printf(" %d ",a[i]);
    getch();
    }

    THE ABOVE CODE REPRESENTS WHICH SORTING TECHNIQUE????

    DOES IT REPRESENT INSERTION SORT????

    Reply
  6. m confused in insertion sort …..when comparison is done and 1st pass completes then how elements compare with all the previous elements there is no loop ……pls explain…..

    Reply
  7. Chandrakanth

    Well explained. I assume there is one error in the code during runtime. Changed below code.

    while( j>=0 && Temp<A[j] )
    {
    A[j+1] = A[j];
    j = j-1;
    }

    Reply
  8. #include
    using namespace std;
    int main()
    { int k,A[6];
    for(int i=0;i>A[i];
    }
    for(int i=1;i=0 && A[j]>A[i])
    {
    A[j+1]=A[j];
    j–;
    }
    A[j+1]=value;
    }
    cout<<"sorted list"<<endl;
    for( k=0;k<6;k++)
    {
    cout<<A[k]<<"\t";
    }
    return 0;
    }
    /* wht give the wrong output?? i tryed to check it …plz help*/

    Reply

Leave a Reply