From 5189c87881273a1d8c730323d8ef7d1195c37c14 Mon Sep 17 00:00:00 2001 From: Sarvagya Saxena Date: Wed, 1 Nov 2023 19:42:34 +0530 Subject: [PATCH 1/2] Add Binary Search Solution in Python --- Python/BinarySearch.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Python/BinarySearch.py diff --git a/Python/BinarySearch.py b/Python/BinarySearch.py new file mode 100644 index 0000000..2ab18d8 --- /dev/null +++ b/Python/BinarySearch.py @@ -0,0 +1,18 @@ + +def binary_search(arr, x, low, high): + if high < low: + return -1 + mid = (high + low) // 2 + if arr[mid] == x: + return mid + elif arr[mid] > x: + return binary_search(arr, x, low, mid - 1) + else: + return binary_search(arr, x, mid + 1, high) + + +arr = [2, 3, 4, 6, 10, 18] +x = 10 +# O/P = 4 + +print(binary_search(arr, x, 0, len(arr) - 1)) From 85a17904f0a6ebd1c7c0c3ddf6e8c4401f533445 Mon Sep 17 00:00:00 2001 From: Sarvagya Saxena Date: Wed, 1 Nov 2023 19:50:12 +0530 Subject: [PATCH 2/2] Add Binary Search in Python --- Python/BinarySearch.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Python/BinarySearch.py b/Python/BinarySearch.py index 2ab18d8..eca2322 100644 --- a/Python/BinarySearch.py +++ b/Python/BinarySearch.py @@ -13,6 +13,5 @@ def binary_search(arr, x, low, high): arr = [2, 3, 4, 6, 10, 18] x = 10 -# O/P = 4 print(binary_search(arr, x, 0, len(arr) - 1))