C PROGRAM TO IMPLEMENT INSERTION SORT.

Share

SOURCE CODE

#include <stdio.h>
int main()
{
   int n, i, j, temp;
   int arr[64];
   printf("\n\t INSERTION SORT\n");
   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])
            {
                temp     = arr[j];
                arr[j]   = arr[j-1];
                arr[j-1] = temp;
                j--;
            }
    }
    printf("Sorted list in ascending order:\n");
    for (i = 0; i <= n - 1; i++)
    {
        printf("\t %d\n", arr[i]);
    }
    
    return 0;
}

OUTPUT

Share
Sudeep Mishra

Sudeep Mishra

Healing

%d bloggers like this: