About Me

My photo
Raipur, Chhattisgarh, India
Hi , I am Amit Thakur. I have worked as a QA Engineer for two years and as a Java Developer for one year in NIHILENT TECHNOLOGIES PVT. LTD., Pune.Currently I am working as DEAN (Research & Development) in Bhilai Institute of Technology, Raipur.

Tuesday, September 3, 2013

C Program to Implement Insertion Sort in C Programming

#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

No comments: