0

I'm using a recursive function for pset3's binary search. How do you call the same function again giving half the list as an argument?

1 Answer 1

2

Make a function declared as bool binsearch(int array[], int start, int end, int ele), with start being the starting index and end being the last index(exclusive), and ele is the element to be search in the array.

Initially call the function as binsearch(array, 0, len, ele), where len is the length of array. Now depending upon the value at mid element, the recursive call goes like this

if (array[mid] == ele)
     return true;
else if (array[mid] < ele)
     return binsearch(array, mid, end, ele);
else
     return binsearch(array, start, mid, ele)

Although I haven't sliced array but I kept passing 2 indices of the array as function parameter so that I can constantly narrow down my search area in array, with one index marking from where should I search and the other till where should I stop searching.

However if you really want to slice the array(something like array[start:mid:] or array[mid:end:] in python) then you should read this answer although it will require slight modifications.

1
  • Worked for me! Thanks
    – pythonhax
    Commented Feb 1, 2015 at 14:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .