Глядя на этот пример от geek для фанатов в python, (здесь показано только двоичное определение поиска), я не понимаю первое утверждение if / else. Учитывает ли это случай, когда границы «начала, конца» БС равны, и определяет, вставляем ли мы в этот случай слева или справа? Если это так, то из комментариев кода не понятно почему. Спасибо
def binary_search(arr, val, start, end):
# we need to distinugish whether we should insert
# before or after the left boundary.
# imagine [0] is the last step of the binary search
# and we need to decide where to insert -1
if start == end:
if arr[start] > val:
return start
else:
return start+1
# this occurs if we are moving beyond left's boundary
# meaning the left boundary is the least position to
# find a number greater than val
if start > end:
return start
mid = (start+end)/2
if arr[mid] < val:
return binary_search(arr, val, mid+1, end)
elif arr[mid] > val:
return binary_search(arr, val, start, mid-1)
else:
return mid