Insertion Sort
#include <stdio.h>
int main()
{
int n, array[1000], i, d, t;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
for (i= 1 ; i <= n - 1; i++)
{
d = i;
while ( d > 0 && array[d-1] > array[d])
{
t = array[d];
array[d] = array[d-1];
array[d-1] = t;
d--;
}
}
for (i = 0; i <= n - 1; i++)
{
printf("%d\n", array[i]);
}
return 0;
}
int main()
{
int n, array[1000], i, d, t;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
for (i= 1 ; i <= n - 1; i++)
{
d = i;
while ( d > 0 && array[d-1] > array[d])
{
t = array[d];
array[d] = array[d-1];
array[d-1] = t;
d--;
}
}
for (i = 0; i <= n - 1; i++)
{
printf("%d\n", array[i]);
}
return 0;
}
Comments
Post a Comment