diff --git a/searches/binary_search_iterative.py b/searches/binary_search_iterative.py new file mode 100644 index 000000000000..19129a8c0ab3 --- /dev/null +++ b/searches/binary_search_iterative.py @@ -0,0 +1,18 @@ +def binary_search(arr, target): + left, right = 0, len(arr) - 1 + while left <= right: + mid = (left + right) // 2 + if arr[mid] == target: + return mid + elif arr[mid] < target: + left = mid + 1 + else: + right = mid - 1 + return -1 + + +if __name__ == "__main__": + sample_list = [1, 3, 5, 7, 9, 11] + target_value = 7 + result = binary_search(sample_list, target_value) + print("Target found at index:", result)