Find Min with Recursion

Started by
32 comments, last by alvaro 10 years, 2 months ago

So I am trying to create an algorithm in C that finds the Min number of an array A[n] using recursion.

What the algorithm does is split the array to 2 equal subsections until the subsections become with only one position left. Until n = 1 so min = A[0]. Then it goes back and the subsectopms start comparing each other.



This is where I have gotten so far.


#include <stdio.h>
#define N 10

int min_num(double T[],int n);

int main()
{
	double T[]={4,5,10,5,7,3,6,2,8,9};
	double min;
	min = min_num(T,N);

	printf("min = %g\n",min);
	return 0;
}

int min_num(double T[],int n)
{

	double min; // the min number
	if (n == 1)
		min = T[0];

	else
	{
		int k;
		k = n/2;
		double SubsectionA_T[k];
		double SubsectionB_T[n-k];

		int i;
		double x,y;
		for (i=0;i<k;i++)
			SubsectionA_T[i] = T[i];
		for (i=k; i<N; i++)
			SubsectionB_T[i] = T[i];

		x = min_num(SubsectionA_T, k);
		y = min_num(SubsectionB_T, n-k);
		min = x;
		if (x > y)
			min = y;
	}
	return min;
}

Failure is not an option...

Advertisement

Oh, geez! Don't make copies of the data! All you need to do is pass pointers into the original data in the recursive call.

Why are you passing k+1 to the recursive call for the second half?

[EDIT: Also, you are happily writing to SubsectionB_T outside the bounds of the array...]

Yeah true... Too many unnecessary mistakes... I will take a look at the code again. tongue.png

Failure is not an option...

So I repost the code


#include <stdio.h>
#define N 10

int min_num(double T[],int n);
void subsections(double T[], int n, int k, double SubsectionA_T[], double SubsectionB_T[]);

int main()
{
	double T[]={4,5,10,5,7,3,6,2,8,9};
	double min;
	min = min_num(T,N);

	printf("min = %g\n",min);
	return 0;
}

int min_num(double T[],int n)
{

	double min;
	if (n == 1)
		min = T[0];

	else
	{
		int k;
		k = n/2;


		double SubsectionA_T[k];
		double SubsectionB_T[n-k];

		double x,y;

		subsections( T, n, k,SubsectionA_T, SubsectionB_T);

		x = min_num(SubsectionA_T, k);
		y = min_num(SubsectionB_T, k-n);
		min = x;
		if (x > y)
			min = y;
	}
	return min;
}

void subsections(double T[], int n, int k, double SubsectionA_T[], double SubsectionB_T[])
{
	int i;
	int j = 0;
	for (i=0;i<k;i++)
		SubsectionA_T[i] = T[i];
	for (i=k; i<n; i++)
	{
		SubsectionB_T[j] = T[i];
		j++;
	}
}

The void function has problems with the array index again...(I think)

Failure is not an option...

You are making it way too complicated. You don't need SubsectionA_T or SubsectionB_T at all. Just call min_num with a pointer into the same array that was passed to the function. Do you know how pointer arithmetic works?

Well I think I know. For example if: int *p points to 1004 place in memory.

The *(p+1) will point on 1008. This is what you mean?

Failure is not an option...

Sure, that's enough. You should be able to solve the problem without copying the data to other buffers.


*edit: see below

He wasn't asking for a solution.

But here's mine: ;)

#include <stdio.h>

double min(double a, double b) {
  return a < b ? a : b;
}

double recursive_min(double *data, unsigned n) {
  return n==1 ? data[0] : min(recursive_min(data, n/2), recursive_min(data + n/2, n - n/2));
}

int main() {
  double a[13] = {31.0, 26.0, 23.0, 15.0, 42.0, 23.0, 53.0, 28.0, 12.0, 40.0, 45.0, 20.0, 39.0};
 
  printf("%lf\n", recursive_min(a, 13));
}

He wasn't asking for a solution.

But here's mine: ;)


#include <stdio.h>

double min(double a, double b) {
  return a < b ? a : b;
}

double recursive_min(double *data, unsigned n) {
  return n==1 ? data[0] : min(recursive_min(data, n/2), recursive_min(data + n/2, n - n/2));
}

int main() {
  double a[13] = {31.0, 26.0, 23.0, 15.0, 42.0, 23.0, 53.0, 28.0, 12.0, 40.0, 45.0, 20.0, 39.0};
 
  printf("%lf\n", recursive_min(a, 13));
}

But that would go into a infinite recursion on a zero sized array ;) I was trying to keep mine a bit more readable. I didn't notice he didn't want a solution, my apologizes, I will erase my post.

This topic is closed to new replies.

Advertisement