Skip to main content

Insertion Sort

 


#include <stdio.h>

int main()
{
    int i, a[10], n, j, temp;
    printf("Enter the length of the array: ");
    scanf("%d", &n);
    for (i = 0; i < n; i++)
    {
        printf("Enter element [%d]: ", i);
        scanf("%d", &a[i]);
    }
    for (i = 0; i < n; i++)
    {
        temp = a[i];

        j = i - 1;
        while (j >= 0 && a[j] > temp)
        {
            a[j + 1] = a[j];
            j--;
        }

        a[j + 1] = temp;
    }

    printf("\n\n");
    for (i = 0; i < n; i++)
    {
        printf(" %d ", a[i]);
    }

    return 0;
}

Comments