FAQ: Binary Search: Python - Recursive Binary Search: The Recursive Steps

This community-built FAQ covers the “Recursive Binary Search: The Recursive Steps” exercise from the lesson “Binary Search: Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Search Algorithms

FAQs on the exercise Recursive Binary Search: The Recursive Steps

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I am not understanding how the result is calculated in the recursive code for (result + mid_idx + 1). Mid_idx is fixed at half of the sorted_list. 1 is added to the zero based index. Result should always be 1, as the last iteration of binary_search? So if Result is always 1, then a right hand value would not work, because (1 + Mid_idx + 1) < index. Unless result is being stored somehow dynamically? Thank you.

Users may find the course on recursion helpful. It helped me see that mid_idx fluctuates with each function call.

This coarse in Python is good. if anybody is wondering . What the program word Node is doing. It writes the y and x in memory. x,y, O(n)…cx, dx… registers of CPU…edon. thus Node. THANKS

I don’t understand why we have to save the recursive call to result:

result = binary_search(right_half, target)
    if result == "value not found":
      return result
    return result + mid_idx + 1

Why this:
result == binary_search(right_half, target)
Why do I save the recursive call to a variable?

Why this:
return result + mid_idx + 1
Why do I return result(I expect that) but with an extra mid_idx and 1?

Why doesn’t this save it to a variable and return an extra mid_idx and 1:

if mid_val > target:
    left_half = sorted_list[:mid_idx]
    return binary_search(left_half, target)


1 Like

Its because when you are meeting the condition of using the right half of your original list, you are taking the a slice of that list starting from the mid index of the original list plus one index. Now the right half of your list is a new list with different indeces for the same values seen in the original list. For example, you are searching for 5 in the list [0,1,2,3,4,5,6,7] which is at index 6. Going through the function you will need use recursion to slice the list and take the right half [4,5,6,7] but now your target value of 5 is at index 2. To reference the original list, you would have to take this into consideration so saving it to result and adding mid_idx+1 will give you the reference to the original list

can someone answer this???

ok thanks its starting to make sense, from the lesson, result returns 0 after recursive call on the right half, then the mid_idx is 2, so its (0 + 2 + 1) = 3, and index of 16 in original list is 3.

To anyone who still doesnt get it, first experiment returning without adding result to mid_idx + 1, you’ll see you only get 0. But thats not the index of 16 in the original list.