Insertioin Sort Code

//Insertion Sort

#include<stdio.h>
int a[100],n,i;
main()
{
void insertionSort(int*);
printf("Enter Size of Array\n");
scanf("%d",&n);
printf("Enter Array Elements\n");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
insertionSort(a);
printf("Sorted Array\n");
for(i=0;i<5;i++)
{
printf("%d ",a[i]);
}
}
void insertionSort(int*a)
{
int i,j,temp;
for(i=0;i<n;i++)
{
temp=a[i];
for(j=i-1;j>=0&&temp<a[j];j--)
{
a[j+1]=a[j];
}
a[j+1]=temp;
}
}

No comments:

Post a Comment