Monday, April 29, 2013

insertion sort in C++

#include<stdio.h>
int main(){

  int m,n,s,temp,a[20];

  printf("Enter how many elements you want to sort? ");
  printf ("\n\n");
  scanf("%d",&s);

  printf("Enter that %d elements: ",s);
  printf ("\n");
  for(m=0;m<s;m++)
      scanf("%d",&a[m]);

  for(m=1;m<s;m++){
      temp=a[m];
      n=m-1;
      while((temp<a[n])&&(n>=0)){
      a[n+1]=a[n];
          n=n-1;
      }
      a[n+1]=temp;
  }

  printf("The elements are sorted : ");
  for(m=0;m<s;m++)
      printf(" %d\n\n",a[m]);

  return 0;
}


No comments:

Post a Comment