C program for insertion sort

source code:


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

Comments

Popular posts from this blog

8086 STRING MANIPULATION –FIND AND REPLACE A WORD

Animated Circles In C++

C program to find out the sum of series 1 + 2 + …. + n.