Insertion sort in C || Arrays || C programming

In this, we are going to see a program in which we will Sort the array in the ascending order using insertion sort method in C programming language.


//Sorts the array in the ascending order using insertion sort method.

#include <stdio.h>
void main()
{
    int i, j, data;
    int x[50], n;
    printf("Enter how many numbers you want to sort out of 50: ");
    scanf("%d", &n);
    printf("Enter %d numbers to sort in asccending order:\n", n);
    for (i = 0; i < n; i++)
    {
        scanf("%d", &x[i]);
    }

    for (i = 0; i < n; i++)
    {
        data = x[i];
        for (j = i - 1; (j >= 0) && (data < x[j]); j--)
        {
            x[j + 1] = x[j];
        }
        x[j + 1] = data;
    }

    printf("The sorted array is....\n");
    for (i = 0; i < n; i++)
    {
        printf("\t%d", x[i]);
    }
}

#ENJOY CODING


Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post