Sum,Min,Max,Mergesort for array   Leave a comment


I was just learning pointers to functions and i wrote those snippets

#include <stdio.h>
#include <stdlib.h>
//Sums two integers a and b
int sum(int a,int b)
{
	return a+b;
}
//returns the minimum of two integers a and b
int min(int a,int b)
{
	return (a<b)?a:b;
}
//returns the maximum of two integers a and b
int max(int a,int b)
{
	return (a>b)?a:b;
}
//Recursive function takes a pointer to function operation and returns
//the result of performing such an operation on an array
int func(int i,int*a,int top,int (*operation)(int x,int y))
{
	if(i==top-1)
		return a[i];
	else
		return operation(a[i],func(i+1,a,top,operation));
}
//iterative function takes a pointer to function operation and returns
//the result of performing such an operation on an array
int func_iter(int *a,int top,int (*operation)(int x,int y))
{
	int i;
	int res = a[0];
	for(i=1;i<top;i++)
	{
		res = operation(res,a[i]);
	}
	return res;
}
//returns the maximum of an array
int max_a(int*a,int len)
{
	return func(0,a,len,&max);
}
//returns the minimum of an array
int min_a(int*a,int len)
{
	return func(0,a,len,&min);
}
//returns the sum of an array
int sum_a(int *a,int len)
{
	return func(0,a,len,&sum);
}
//returns the maximum of an array iterative
int sum_a_iter(int *a,int len)
{
	return func_iter(a,len,&sum);
}
//returns the minimum of an array iterative
int min_a_iter(int*a,int len)
{
	return func_iter(a,len,&min);
}
//returns the max of an array iterative
int max_a_iter(int*a,int len)
{
	return func_iter(a,len,&max);
}
//returns factorial of n recursive
int factorial(int n)
{
	if(!n)
		return 1;
	else
		return n*factorial(n-1);
}
//returns the factorial of n iterative
int factorial_iterative(int n)
{
	int i;
	int res = 1;
	for(i=2;i<=n;i++)
		res*=i;
	return res;
}
//ascending order function required for merge sort algorithm
int ascending(int a,int b)
{
	return a<b;
}
//descending order function required for merge sort algorithm
int descending(int a,int b)
{
	return a>b;
}
//Merger sorter should be called with pointer to order function
//weather the result should be sorted ascending or descending order
void mergeSort(int*array,int len,int(*order_fn)(int a,int b))
{
	if(len!=1)
	{
		int*a,*b,i,j,mid,top;
		mid = len/2;
		a = (int*) malloc(mid*sizeof(int));
		b = (int*) malloc((len-mid)*sizeof(int));
		for(i=0;i<mid;i++)
			a[i] = array[i];
		for(i=0;i+mid<len;i++)
			b[i] = array[i+mid];
		mergeSort(a,mid,order_fn);
		mergeSort(b,len-mid,order_fn);
		i = j = top = 0;
		while(i<mid&&j<len-mid)
			if(order_fn(a[i],b[j]))
				array[top++] = a[i++];
			else
				array[top++] = b[j++];
		for(;i<mid;i++)
			array[top++] = a[i];
		for(;j<len-mid;j++)
			array[top++] = b[j];
	}
}

int main()
{
	int i;
	int a[7] = {1,-2,3,632,4,5,7};
	mergeSort(a,7,&ascending);
	for(i=0;i<7;i++)
		printf("%d ",a[i]);
	//OUTPUTS -2 1 3 4 5 7 632
	return 0;
}
Advertisements

Posted July 16, 2011 by epicrado in C

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

%d bloggers like this: