C Program for Insertion Sort

Write a Program to Sort an 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 insertion sort 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.

#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 that C Program will enter into the 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.

Insertion Sort Program in C using While Loop

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

// using a while loop
#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 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

 Result :  -22 	 -9 	 0 	 2 	 7 	 12 	

C Program for Insertion Sort using Functions

This insertion program uses Functions to sort array elements.

#include <stdio.h>
void insFunc(int a[], int num) {
    int i, j, temp;
    for(i = 1; i <= num - 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], num, i;
    
    printf("\n Please Enter the total No. of Elements  :  ");
    scanf("%d", &num);
    
    printf("\n Please Enter the Array Elements  :  ");
    for(i = 0; i < num; i++)
        scanf("%d", &a[i]);

    insFunc(a, num);
    printf("\n Output : ");
    for(i = 0; i < num; i++)  {
        printf(" %d \t", a[i]);
    }
    printf("\n");
    return 0;
}
Please Enter the total No. of Elements  :  5

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

 Output :  -22 	 -3 	 2 	 5 	 10