From 22a4c28a37f9e5846c17c09819db86cb6e39aead Mon Sep 17 00:00:00 2001 From: Sundram_Kumar Date: Wed, 15 Oct 2025 19:59:55 +0530 Subject: [PATCH 1/2] Add my BinarySearch algo --- Binary_search.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Binary_search.py diff --git a/Binary_search.py b/Binary_search.py new file mode 100644 index 0000000..9003352 --- /dev/null +++ b/Binary_search.py @@ -0,0 +1,44 @@ +# Binary Search Algorithm in Python + +def binary_search(arr, target): + """ + This function searches for 'target' inside the sorted list 'arr'. + If found, it returns the index of the target. + If not found, it returns -1. + """ + + # Step 1: Set the starting and ending positions + left = 0 + right = len(arr) - 1 + + # Step 2: Keep checking while there is still a search space + while left <= right: + # Step 3: Find the middle index + mid = (left + right) // 2 + + # Step 4: Check if the middle element is the target + if arr[mid] == target: + return mid # Found the target + + # Step 5: If target is smaller, search in the left half + elif arr[mid] > target: + right = mid - 1 + + # Step 6: If target is bigger, search in the right half + else: + left = mid + 1 + + # Step 7: If we exit the loop, the target was not found + return -1 + + +# Example Usage +numbers = [1, 3, 5, 7, 9, 11, 13] +target_number = 7 + +result = binary_search(numbers, target_number) + +if result != -1: + print(f"Number {target_number} found at index {result}") +else: + print(f"Number {target_number} not found in the list") From 1f35e89c56cbc71cc603d62a7890015576df7d6d Mon Sep 17 00:00:00 2001 From: Sundram_Kumar Date: Wed, 15 Oct 2025 20:01:48 +0530 Subject: [PATCH 2/2] Add my Binary Search Algo --- CONTRIBUTORS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 44caf85..8223449 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -2,4 +2,4 @@ Thanks to everyone contributing to this repository! -- [Your Name Here] +-Sundram Kumar