Insertion sort algorithm implementation in c
#include <stdio.h>
#include<conio.h>
void main()
{
int n, array[1000], i, j, t;
clrscr();
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++)
{
j = i;
while ( j > 0 && array[j] < array[j-1])
{
t = array[j];
array[j] = array[j-1];
array[j-1] = t;
j--;
}
}
printf("Sorted list in ascending order:\n");
for (i = 0; i <= n - 1; i++)
{
printf("%d\n", array[i]);
}
getch();
}
No comments:
Post a Comment