Insertion sort
/* Sort an array using Insertion sort */
#include<stdio.h>
int main()
{
int a[100],n,m,i,t;
printf("Enter the number of elements : ");
scanf("%d",&m);
printf("Enter the values : ");
for(i=0;i<m;i++)
{
scanf("%d",&a[i]);
}
printf("\nValues in the array are : ");
for(i=0;i<m;i++)
printf("%d,",a[i]);
for(i=0;i<m;i++)
{
t=i-1;
n=a[i];
while(t>=0 && a[t]>n)
{
a[t+1]=a[t];
t--;
}
a[t+1]=n;
}
printf("\nAfter arrangement : ");
for(i=0;i<m;i++)
printf("%d,",a[i]);
printf("\n\n\n");
}
#include<stdio.h>
int main()
{
int a[100],n,m,i,t;
printf("Enter the number of elements : ");
scanf("%d",&m);
printf("Enter the values : ");
for(i=0;i<m;i++)
{
scanf("%d",&a[i]);
}
printf("\nValues in the array are : ");
for(i=0;i<m;i++)
printf("%d,",a[i]);
for(i=0;i<m;i++)
{
t=i-1;
n=a[i];
while(t>=0 && a[t]>n)
{
a[t+1]=a[t];
t--;
}
a[t+1]=n;
}
printf("\nAfter arrangement : ");
for(i=0;i<m;i++)
printf("%d,",a[i]);
printf("\n\n\n");
}
Comments
Post a Comment