Implement insertion sort.
#include <stdio.h>
int main()
{
int n, i, j, temp;
int numbers[64];
printf("Enter number of numbers\n");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("Enter number %d: ", (i+1));
scanf("%d", &numbers[i]);
}
for (i = 1 ; i <= n - 1; i++)
{
j = i;
while ( j > 0 && numbers[j-1] > numbers[j])
{ Â Â Â Â Â
temp   = numbers[j];
numbers[j]Â Â = numbers[j-1];
numbers[j-1] = temp;
j--;
}
}
printf("Sorted list in ascending order:\n");
for (i = 0; i <= n - 1; i++)
{
printf("%d\n", numbers[i]);
}
getchar();
getchar();
return 0;
}
Comments
Leave a comment