5th sem ADA programm to implement sorting algorithms


  •  Insertion sort in c


#include<stdio.h>
void insertion(int n,int arr[]);
void main()
{
    int n,i;
    printf("enter the size of array:");
    scanf("%d",&n);
    int arr[n];
    printf("enter the array elements:\n");
    for(i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }
    insertion(n,arr);
    printf("\nyour sorted array is:\n");
    for(i=0;i<n;i++)
    {
        printf("%d\n",arr[i]);
    }
}
void insertion(int n,int arr[n])
{
    int i,j,swap;
    for(i=1;i<n;i++)
    {
        j=i;
        while(j>0 && arr[j] < arr[j-1])
        {
            swap=arr[j];
            arr[j]=arr[j-1];
            arr[j-1]=swap;
            j--;
        }
    }
}

Comments

Popular posts from this blog

5th sem OOPJ Write an interactive program to print a diamond shape. For example, if user enters the number 3, the diamond will be as follows:

SQL Lab 3

SQL Lab 6