Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Coding/Python/BinarySearch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Binary Search implementation in Python

def binary_search(arr, target):
low = 0
high = len(arr) - 1

while low <= high:
mid = (low + high) // 2 # Find the middle index
if arr[mid] == target:
return mid # Target found, return index
elif arr[mid] < target:
low = mid + 1 # Search the right half
else:
high = mid - 1 # Search the left half

return -1 # Target not found

# Example usage
data = [2, 4, 6, 8, 10, 12, 14, 16, 18]
target = 10
result = binary_search(data, target)

if result != -1:
print(f"Element {target} found at index {result}.")
else:
print(f"Element {target} not found in the list.")