Find Min with Recursion

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

Right laugh.png

Failure is not an option...

Advertisement

Yes, returning the index is a good solution:


unsigned recursive_min(double *data, unsigned n) {
  if (n <= 1) // [EDIT: Was `n == 1'. Thanks, rip-off.]
    return n - 1; // returns -1 for an empty input
  unsigned left_index = recursive_min(data, n/2);
  unsigned right_index = recursive_min(data + n/2, n - n/2) + n/2;
  return data[left_index] < data[right_index] ? left_index : right_index;
}


// returns -1 for an empty input

It would, if the test were n <= 1.


It would, if the test were n <= 1.

Ooops! Brain fart! :)

This topic is closed to new replies.

Advertisement