Program to Implement Insertion Sort in C Programming

January 18, 2025 2 Comments » Hits : 241





Program :

#include<stdio.h>
int main(){
  int i,j,num,temp,a[20];
  printf("Enter total elements: ");
  scanf("%d",&num);
  printf("Enter %d elements: ",num);
  for(i=0;i<num;i++)
      scanf("%d",&a[i]);
  for(i=1;i<num;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("After Sorting: ");
  for(i=0;i<num;i++)
      printf("%d",a[i]);
  return 0;
}

Output :

Enter total elements: 5
Enter 5 elements: 9 4 1 0 2
After sorting:  0 1 2 4 9

[/toggle]

Download Program

Incoming search terms:

  • Marco Stenstrom

    Good info. Lucky me I reach on your website by accident, I bookmarked it.

  • Romana Laforrest

    Highly energetic blog, I enjoyed that a lot. Will there be a part 2?