C Program for Insertion Sort

Write a Program to Sort Array using Insertion sort in C using For Loop, While loop, and Functions with a practical example.

C Program for Insertion Sort using For Loop

This program allows the user to enter the array size and the One Dimensional Array row elements. Next, we are using Nested For Loop to sort the array elements using insertion sort.

/* C Program for Insertion Sort */
#include <stdio.h>
int main()
{
    int a[100], number, i, j, temp;
    
    printf("\n Please Enter the total Number of Elements  :  ");
    scanf("%d", &number);
    
    printf("\n Please Enter the Array Elements  :  ");
    for(i = 0; i < number; i++)
        scanf("%d", &a[i]);
    
    for(i = 1; i <= number - 1; i++)
    {
        for(j = i; j > 0 && a[j - 1] > a[j]; j--)
        {
                temp = a[j];
                a[j] = a[j - 1];
                a[j - 1] = temp;
        }
    }
    printf("\n Insertion Sort Result : ");
    for(i = 0; i < number; i++)
    {
        printf(" %d \t", a[i]);
    }
    printf("\n");
    return 0;
}
C Program for Insertion Sort 1

First For Loop – First Iteration: for(i = 1; 1 <= 4; 1++)
The condition is True so, C Program will enter into second for loop

Second For Loop – First Iteration: for(j = 1; 1 > 0 && a[0] > a[1]; 1–)
(a[0] > a[1]) => (10 > -20)  – Condition inside the For Loop is True
temp = -20
a[j] = a[j – 1] => a[1] = a[0] => a[1] = 10
a[j – 1] = temp => a[0] = -20
Now the array will be  -20 10 10 3 15.

Do the same for the remaining For Loop Iterations of the One Dimensional Array.

C Program for Insertion Sort using While Loop

In this program, we substituted the for loop with While Loop to sort array elements using insertion sort

/* C Program for Insertion Sort */
#include <stdio.h>
int main()
{
    int a[100], number, i, j, temp;
    
    printf("\n Please Enter the total Number of Elements  :  ");
    scanf("%d", &number);
    
    printf("\n Please Enter the Array Elements  :  ");
    for(i = 0; i < number; i++)
        scanf("%d", &a[i]);
    
    i = 1;
    while( i <= number - 1)
    {
        j = i;
        while( j > 0 && a[j - 1] > a[j])
        {
            temp = a[j];
            a[j] = a[j - 1];
            a[j - 1] = temp;
            j--;
        }
        i++;
    }
    printf("\n Insertion Sort Result : ");
    for(i = 0; i < number; i++)
    {
        printf(" %d \t", a[i]);
    }
    printf("\n");
    return 0;
}
Please Enter the total Number of Elements  :  6

 Please Enter the Array Elements  :  2 12 -9 7 0 -22

 Insertion Sort Result :  -22 	 -9 	 0 	 2 	 7 	 12 	

Insertion Sort in C using Functions

This insertion sort program uses Functions to sort array elements by insertion sort.

/* C Program for Insertion Sort */
#include <stdio.h>
void insertionSort(int a[], int number) {
    int i, j, temp;
    for(i = 1; i <= number - 1; i++)
    {
        for(j = i; j > 0 && a[j - 1] > a[j]; j--)
        {
            temp = a[j];
            a[j] = a[j - 1];
            a[j - 1] = temp;
        }
    }
}
int main()
{
    int a[100], number, i;
    
    printf("\n Please Enter the total Number of Elements  :  ");
    scanf("%d", &number);
    
    printf("\n Please Enter the Array Elements  :  ");
    for(i = 0; i < number; i++)
        scanf("%d", &a[i]);

    insertionSort(a, number);
    printf("\n Insertion Sort Result : ");
    for(i = 0; i < number; i++)  {
        printf(" %d \t", a[i]);
    }
    printf("\n");
    return 0;
}
Please Enter the total Number of Elements  :  5

 Please Enter the Array Elements  :  10 5 -3 2 -22

 Insertion Sort Result :  -22 	 -3 	 2 	 5 	 10