0

This is a C program with the recursive binary search algorithm, however when I run it, the debugger says there is an access segmentation fault in the binary search function. Why is this and how do I fix this?

Here is the recursive binary search function:

int binSearch(int val, int numbers[], int low, int high)                 
{
     int mid;

     mid=(low+high)/2;
     if(val==numbers[mid])
     {  
                return(mid);          
     }   
     else if(val<numbers[mid])
     {
                return(binSearch(val, numbers, low, mid-1));               
     }            
     else if(val>numbers[mid])
     { 
                return(binSearch(val, numbers, mid+1, high));  
     }    
     else if(low==high)
     {
                return(-1);    
     }
}

Thank you :)

4
  • 1
    Do you know where exactly it is seqfaulting? Can you try to move the last condition to the top of the function and see if it fixes it? Commented Jul 24, 2013 at 9:40
  • How do you call the function? Where is the crash (at which line)? Can you please show the debugger backtrace? Commented Jul 24, 2013 at 9:40
  • 3
    You do not ensure that low is actually lower than hight. Add a test for that. Commented Jul 24, 2013 at 9:42
  • the caller and the error message will be helpful Commented Jul 24, 2013 at 9:44

4 Answers 4

5

You must check low == high before val < ... and val > ... because otherwise high could become less than low and so your next recursion might calculate an invalid mid

Sign up to request clarification or add additional context in comments.

3 Comments

what if low > high at the first place
@mathk I would call that an invalid call to binSearch() so it should be enough to require low <= high in the function's doc. But of course you could check it first if you preferred.
this is basic security check. Never trust the input.
2

Your edge cases are off: specifically, when your low and high indices pass, you continue to call recursively before you reach the low == high test. Rearrange the tests:

int binSearch(int val, int numbers[], int low, int high) {
    int mid = (low + high) / 2;

    if (val == numbers[mid]) return mid;

    if (val < numbers[mid]) {
        if (mid > low) return binSearch(val, numbers, low, mid-1);
    } else if (val > numbers[mid]) {
        if (mid < high) return binSearch(val, numbers, mid+1, high);
    }
    return -1;
}

Comments

0

Try this:

Fixed if constructs in your code

int binSearch(int val, int numbers[], int low, int high)                 
{
     int mid;

     mid=(low+high)/2;

     if(low<=high)
     {
         if(val==numbers[mid])
           return mid;          

         else if(val<numbers[mid])
           return binSearch(val, numbers, low, mid-1);

        else 
          return binSearch(val, numbers, mid+1, high);  
     }
     else
           return -1;    

}

Comments

0

low < high is not ensure. If that is not the case your are going to search out of the array bound.

Add a sanity check for that.

if (low < high)
     return -1;

EDIT: as other point out you can also check if low == high at the beginning but that does not ensure that the first call of the function have sound value.

1 Comment

Normally, the low index shall be less than the high index, so what is the case to be covered by this condition?!

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.