Data Structure Insertion Sort

program of insertion sorting is given below:

//Insertion sorting
#include<stdio.h>
void main()
{
    int B[50],n,temp;
    printf("Enter the size of array :");
    scanf(" %d",&n);
    printf("Enter the array:\n");
    for(int i=0;i<n;i++)
    {
        scanf(" %d",&B[i]);
    }
    for(int i = 1;i<n;i++)
    {
        temp=B[i];
        int j =i-1;
        while(j>=0 && B[j]>temp)
        {
            B[j+1]=B[j];
            j--;
        }
        B[j+1]=temp;
    }
    printf("The sort array is :");
    for(int i=0;i<n;i++)
    {
        printf(" %d",B[i]);
    }
}

Output

Enter the size of array :4
Enter the array:
11
67
98
2
The sort array is : 2 11 67 98

Leave a Reply

Your email address will not be published. Required fields are marked *

Solverwp- WordPress Theme and Plugin