Insertion Sort

#include<stdio.h>
#include<conio.h>
void insertionSort(int a[],int n);
int main ()
{
	int n,i,a[10];
   clrscr();
   printf("How many no you want to enter: ");
   scanf("%d",&n);
   printf("Enter the no:\n");
   for(i=0;i<n;i++)
   	scanf("%d",&a[i]);
   insertionSort(a,n);
   printf("After Insertion Sorting:\n");
   for(i=0;i<n;i++)
   	printf("%d ",a[i]);
   getch();
   return 0;
}

void insertionSort(int a[],int n)
{
	int i,j,temp;
   for(i=0;i<n;i++)
   {
   	temp=a[i];
      j=i-1;
      while((a[j]>temp)&&(j>=0))
      {
      	a[j+1]=a[j];
         j--;
      }
      a[j+1]=temp;
   }
}

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *