This function carries out Binary search on a 1d array and return -1 if it do not exist array: A 1d sorted array value : the value meant to be searched >>> matrix = [1, 4, 7, 11, 15] >>> binary_search(matrix, 0, len(matrix) - 1, 1) 0 >>> binary_search(matrix, 0, len(m
(array: list, lower_bound: int, upper_bound: int, value: int)
| 1 | def binary_search(array: list, lower_bound: int, upper_bound: int, value: int) -> int: |
| 2 | """ |
| 3 | This function carries out Binary search on a 1d array and |
| 4 | return -1 if it do not exist |
| 5 | array: A 1d sorted array |
| 6 | value : the value meant to be searched |
| 7 | >>> matrix = [1, 4, 7, 11, 15] |
| 8 | >>> binary_search(matrix, 0, len(matrix) - 1, 1) |
| 9 | 0 |
| 10 | >>> binary_search(matrix, 0, len(matrix) - 1, 23) |
| 11 | -1 |
| 12 | """ |
| 13 | |
| 14 | r = int((lower_bound + upper_bound) // 2) |
| 15 | if array[r] == value: |
| 16 | return r |
| 17 | if lower_bound >= upper_bound: |
| 18 | return -1 |
| 19 | if array[r] < value: |
| 20 | return binary_search(array, r + 1, upper_bound, value) |
| 21 | else: |
| 22 | return binary_search(array, lower_bound, r - 1, value) |
| 23 | |
| 24 | |
| 25 | def mat_bin_search(value: int, matrix: list) -> list: |