Question

Write a program to sort a list of numbers using insertion sort.

Source Code

#include <stdio.h>

void accept(int Arr[], int s);
void display(int Arr[], int s);
void isort(int Arr[], int s);

int main()
{
    int list[100], n;
    printf("Enter size of Array : ");
    scanf("%d", &n);
    accept(list, n);
    printf("\nBefore sorting");
    display(list, n);
    isort(list, n);
    printf("\nAfter sorting");
    display(list, n);

    return 0;
}

void accept(int Arr[], int s)
{
    int i;
    for (i = 0; i < s; i++)
    {
        printf("Enter element %d : ", i + 1);
        scanf("%d", &Arr[i]);
    }
}

void display(int Arr[], int s)
{
    int i;
    printf("\n");
    for (i = 0; i < s; i++)
    {
        printf("%d ", Arr[i]);
    }
}

void isort(int Arr[], int s)
{
    int i, j, temp;

    for (i = 1; i < s; i++)
    {
        temp = Arr[i];
        j = i - 1;
        while ((temp < Arr[j]) && (j >= 0))
        {
            Arr[j + 1] = Arr[j];
            j--;
        }
        Arr[j + 1] = temp;
    }
}

Output

Enter size of Array : 5
Enter element 1 : 19
Enter element 2 : 11
Enter element 3 : 12
Enter element 4 : 3
Enter element 5 : 10

Before sorting
19 11 12 3 10
After sorting
3 10 11 12 19