Insertion Sort
/*******************************************************************************/
#include<stdio.h>
void insertionsort(int arr[],int n)
{
int i,j,key;
for(i=1;i<n;i++)
{
key=arr[i];
j=i-1;
while(j>=0 && arr[j] > key)
{
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=key;
}
}
int main(){
int size,i;
printf("Enter size of the array: ");
scanf("%d",&size);
int arr[size];
printf("Enter %d elements: ",size);
for(i=0;i<size;i++)
scanf("%d",&arr[i]);
insertionsort(arr,size);
printf("Sorted elements: ");
for(i=0;i<size;i++)
printf(" %d",arr[i]);
return 0;
}
Comments
Post a Comment