Let's take a trivial example. You have the dataset 1, 3. You search for value 2 in the dataset.
So your function call will pass in the following parameters for first, last, and value: 0, 1, 2.
data[1] = 3, and first is not greater than or equal to last. You thus don't hit either of the recursion terminating return calls.
You value of 2 is less than data[1] of 3, so you call bsearch_recur again with the following parameters: 0, 1, 2.
But wait, that's the same as the first call. You've descended into an infinite recursive spiral that eventually overflows the stack and aborts the executable.
middle is first+last/2. and you need to increase by one or reduce by one somewhere else.
First you need () around the addition and second better use "middle = first + (last-first)/2", because first+last can be already a nasty integer overflow for very large integers.
As with ALL common algorithms: Look at wikipedia. They got pseudo-code.
if i understood u , u want to make a function that does the same thing as function search from libary algorithm?
if that is the case, than yes, there is a more elegant way...
first u send a pointer to the first element of the array, and another pointer that points after the last element, and the value the search needs to find, after that a simple
while loop should do the trick.
I'm pretty sure OP is doing a recursive binary search because the assignment requires it.
Otherwise the whole thing could be done in 1 line of code with std::find().
In particular, look at how the flowchart describes how to generate your index position, and how it uses that index position to determine a "not found" condition.
On a separate subject, your two globals. With the above info you'll be able to eliminate them from your algorith, but for future reference, you could have preferably used two statics in the function call instead of globals.