Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

6
  • What does binary_search([], 1) return and what should it return? Commented Jan 5, 2022 at 2:07
  • you forget to include the mid position in your divide: find_first_index(A,low,mid-1,key) find_first_index(A,mid+1,high,key) Commented Jan 5, 2022 at 2:53
  • Hi David, I did take care of the midpoint. In my solution, first I wanted to find the midpoint of the list. Next, I checked if the midpoint is the first occurrence of number. If so, return the current position of the midpoint. Otherwise, if the midpoint were not the first occurrence of number, i.e., there exists some element number that precedes the midpoint, then I apply the same function recursively on the interval [low,midpoint-1]. Commented Jan 5, 2022 at 3:47
  • That convoluted way of calculating mid is unnecessary in Python, because Python integers can't overflow. Just use mid = (high+low) // 2. Commented Jan 5, 2022 at 4:57
  • Binary search is deceptively simple in theory but hideously hard in practice, see Are you one of the 10% of programmers who can write a binary search? Commented Jan 5, 2022 at 5:04