Ad Code

Responsive Advertisement

Insertion Sort using C language

 Input : 6 2 9 0 3

Output : 0 2 3 6 9

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>

#include <string.h>

int main()

{

    int a[20], i, n;

    printf("Enter the size of Array  : ");

    scanf("%d", &n);

    printf("Enter the unsorted array element : \n");

    for (i = 0; i < n; i++)

    {

        printf("Array index[%d] : ", i);

        scanf("%d", &a[i]);

    }

    insertionSort(a, n); // function calling

    printf("Sorted array element is : \n");

    for (i = 0; i < n; i++)

    {

        printf("\t %d", a[i]);

    }

    getch();

    return 0;

}

void insertionSort(int a[], int n) // 5 2 6 1 9

{

    int i, j, key;

    for (i = 1; i < n; i++)

    {

        key = a[i];

        for (j = i - 1; j >= 0 && key < a[j]; j--)

        {

            a[j + 1] = a[j];

        }

        a[j + 1] = key;

    }

}

Post a Comment

0 Comments

Ad Code

Responsive Advertisement