1

Here is one implementation I did for Binary search

public int FindIndexRecursive(int numberToFind, int[] array, int indexMin, int indexMax)
{
    if (indexMin > indexMax)
    {
        return -1;
    }

    int mid = ((indexMax - indexMin) / 2 ) + indexMin;

    if (array[mid] < numberToFind)
    {                
        return FindIndexRecursive(numberToFind, array, mid, indexMax); 
    }
    else if (numberToFind < array[mid])
    {
        return FindIndexRecursive(numberToFind, array, indexMin, mid);
    }
    else 
    {
        return mid;
    }
}

it works fine. I asked a question https://codereview.stackexchange.com/questions/71815/binary-search-using-recursion-iteration-shifted-array

and I was answer that I can change my mid calculation

from: int mid = ((indexMax - indexMin) / 2 ) + indexMin;
to: int mid = ((indexMax - indexMin) / 2 );

public int FindIndexRecursive2(int numberToFind, int[] array, int indexMin, int indexMax)
{
    if (indexMin > indexMax)
    {
        return -1;
    }

    int mid = ((indexMax - indexMin) / 2);
    //since the array is sorted
    if (numberToFind < array[mid])
    {
        return FindIndexRecursive2(numberToFind, array, indexMin, mid-1);
    }
    else if (numberToFind > array[mid])
    {
        return FindIndexRecursive2(numberToFind, array, mid+1, indexMax);
    }
    else
    {
        return mid;
    }
}

However now I get a stack overflow, what am I missing in my new implementation?

3
  • 2
    Did you mean to write: int mid = (indexMax + indexMin) / 2 ? Commented Dec 20, 2014 at 17:12
  • @MichaelPetito I think this was my problem Commented Dec 20, 2014 at 19:54
  • Check out this answer for the simplest implementation of recursive Binary Search using C# 12. Commented Jan 28, 2024 at 21:14

3 Answers 3

4

The original int mid = ((indexMax - indexMin) / 2 ) + indexMin; computes the distance beween indexMax and indexMin halves it and uses it to index the original array relatively to indexMin.

The new int mid = ((indexMax - indexMin) / 2 ); is wrong and should contain a + instead of the -: int mid = ((indexMax + indexMin) / 2 ); This uses the fact that mean of two values is just in the middle between them.

If you take a pen and write this and the original expression as expressions with fractions, you’ll come to the conclusion that they are equal:

indexMax - indexMin              indexMax - indexMin   2 · indexMin   indexMax + indexMin
------------------- + indexMin = ------------------- + ------------ = -------------------
         2                                2                  2                 2
Sign up to request clarification or add additional context in comments.

Comments

3

You might have been advised that, to reduce the number of operations to compute the mid.

(indexMax - indexMin)/2 + indexMin equals (indexMax + indexMin)/2

1 Comment

@neo; they are by no means equal :P, different formulas, different behaviors.
3

The way you take middle, is (low+high)/2. Otherwise you'll get stack overflow exception.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.