C program to implement insertion sort using function and pointers

Posted: January 28, 2012 in Lab 4
#include<stdio.h>
#include<stdlib.h>
int insertionsort(int*,int);
int insertionsort(int *arr,int n)
{
	int i,j,temp;
for(i=1;i<n;i++)
	{
		temp=*(arr+i);
		j=i-1;
		while(temp<*(arr+j)&&j>=0)
		{
			*(arr+(j+1))=*(arr+j);
			j--;
		}
		*(arr+(j+1))=temp;
	}
	return 0;
}
int main()
{
	int *arr,i,n;
	printf("Enter the no. of elements\n");
	scanf("%d",&n);
	arr=(int*)malloc(n*sizeof(int));
	printf("Enter elements\n");
	for(i=0;i<n;i++)
		scanf("%d",(arr+i));
	insertionsort(arr,n);
	printf("Sorted array is\n");
	for(i=0;i<n;i++)
		printf("%d\n",*(arr+i));
	return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s