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
Post a Comment