Find Min with Recursion

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

I frob missed the trick:

You can process the elements two at a time, compare them and then only use the smaller of the two to try to update the minimum and only use the larger of the two to try to update the maximum. This requires about N*3/2 comparisons.

I am not making any claims about practical usability of this trick: It's just a neat little puzzle.

Meh, it is a micro-optimization in the part that doesn't dominate. My hunch is that the added branch would slow things down if you were worried about performance.

As mentioned, there are solutions that require zero comparisons. Keep the collection sorted in the first place and don't worry about it.

Advertisement
No, it's not a micro-optimization: It's the solution to a brain teaser. I think I posed the question correctly. If you don't enjoy that kind of thing, that's OK. But trying to argue about the real-life complexities of the situation is missing the interesting mathematical curiosity that I was pointing at.

So guys any ideas on how to make the code return except for the value also the position of min?


What part are you having a hard time with?

The problem is that the index keeps changing during the recursions.

And I dont want to make a classical search in order to find where the min is located inside the A[10].

I wanna know if there is a solution on how to return the index without searching.

6z1w5u.png

Failure is not an option...

The easy way is with just searching. Is there another more clever way?


#include <stdio.h>

double recursive_min(double T[], int n,int *index);

int main(){
	double T[10] = {4,50,-10,-6,7,-11.6,7,-1,8,-9};
	double min;
	int index;
	min = recursive_min(T,11,&index);
  
    printf("min = %g\n", min);
    printf("Index = %d\n",index);
	
}

double recursive_min(double T[], int n,int *index){
	int i;
	if (n == 1)	
		return T[0];
	else{
		double x = recursive_min(T,n/2,index);
		double y = recursive_min(T + n/2, n - n/2,index);
		if (x < y)
		{
			for (i = 0; i<=n/2; i++){
				if (T[i] == x) 
					*index = i;
		}
			return x;
		}
		else 
		{
			for (i =n/2 ; i<=n; i++){
				if (T[i] == y) 
					*index = i;
				}
			return y;
		}
	}
}

Failure is not an option...

I wouldn't like that solution as it scans the data many times. You might as well just have an iterative solution at that point. Also, there is a bug, if the function is passed a one element array, the index is not set.

A nicer solution might be to change the signature to accept a range:

double recursive_min(double *array, int start, int end, int *index);

Here, end is "one past the end", i.e. for an array of size N, end would be N.

So now, in the recursion, pass the same array (no pointer arithmetic), but modify the start / end parameters appropriately. Thus, you would then always know the absolute index even as you go deeper in the recursion.

rip-off's suggestion is good.

Alternatively, you can keep the original signature, if you are a little bit careful:

double recursive_min(double *data, unsigned n, unsigned *index) {
  if (n==1) {
    *index = 0;
    return data[0];
  }

  unsigned index_left, index_right;
  double left = recursive_min(data, n/2, &index_left);
  double right = recursive_min(data + n/2, n - n/2, &index_right);

  return left < right ? (*index = index_left, left) : (*index = index_right + n/2, right);
}

Note that returning the index instead of the value can be a good solution. You can handle an empty array by returning an invalid index, such as -1 or N, but client code can still quickly get the value by using the index. For more general searching algorithms, this can handle the case where no matching element is found, too.

thanks :)

Is it necessary to have 2 different index?

Because I think this also works too.


double recursive_min(double T[], int n, int *index){
	if (n == 1){
		*index = 0;
		return T[0];}
	else{
		double x = recursive_min(T, n/2, index);
		double y = recursive_min(T + n/2, n - n/2, index);
		if (x < y){
			return x;}
		else {
			*index = *index + n/2;
			return y;}
	}
}

Failure is not an option...

If the minimum is in the first half, the index will be lost when you search the second half.

This topic is closed to new replies.

Advertisement