Insert Sort (Data Structures in C)
Saturday, October 3, 2020
#include <stdio.h>
#include <conio.h>
int main()
{
int n, i, j, swap;
int arr[80];
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for (i = 1 ; i <= n - 1; i++)
{
j = i;
while ( j > 0 && arr[j-1] > arr[j])
{
swap = arr[j];
arr[j] = arr[j-1];
arr[j-1] = swap;
j--;
}
}
printf("Sorted list in ascending order:\n");
for (i = 0; i <= n - 1; i++)
{
printf("%d\n", arr[i]);
}
return 0;
}