Insertion sort

On-campus and online computer science courses to Learn the basic concepts of Computer Science.This tutorial will cover c ,c++, java, data structure and algorithm,computer graphics,microprocessor,analysis of algorithms,Digital Logic Design and Analysis,computer architecture,computer networks,operating system.
code in C:
#include<stdio.h>
int i,j,n,temp,a[20];
void Insertion_sort()
{
 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;
   }
}

int main()
{
  printf("Enter total elements: ");
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
      printf("\nEnter %d number : ",i+1);
       scanf("%d",&a[i]);
  }
  Insertion_sort();  
  printf("After sorting:\n ");
  for(i=0;i<n;i++)
      printf(" %d",a[i]);
  return 0;
}

output:-
Enter total no. of elements : 5
Enter 1 number : 29
Enter 2 number : 21
Enter 3 number : 64
Enter 4 number : 02
Enter 5 number : 56
After sorting:
2       21      29      56      64

--->Insertion sort program with no of pass ,count and time complexity

0 comments: