pages

Thursday, 2 September 2021

Insertion sorting in c-programming || sorting in c-language

 Insertion sorting

 Insertion sort algorithm picks elements one by one and places it to the right position where it belongs in the sorted list of elements. In the following C program we have implemented the same logic.


#include<stdio.h>
#include<conio.h>
void insertion_sort(int A[],int N);
void main()
{
    int A[10],i;
    printf("enter the 10 numbers:-");
    for(i=0;i<10;i++)
    {
        scanf("%d",&A[i]);
    }
    printf("\n the elements are :-");
    for(i=0;i<10;i++)
    {
        printf("%d ",A[i]);
    }
    printf("\n the sorted elements are *****************//////");
    insertion_sort(A,10);
    for(i=0;i<10;i++)
    {
        printf("%d ",A[i]);
    }
    getch();
}
void insertion_sort(int A[],int N)
{
    int j,i,temp;
    for(i=1;i<10;i++)
    {
        temp=A[i];
        for(j=i-1;j>=0 && temp<A[j];j--)
        {
            A[j+1]=A[j];
        }
        A[j+1]=temp;
    }
}