Skip to content
Closed
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
22 changes: 22 additions & 0 deletions binary_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
def binary_search(arr, x):
arr.sort() # Sort in-place for efficiency
l, r = 0, len(arr) - 1

Check failure on line 3 in binary_search.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E741)

binary_search.py:3:5: E741 Ambiguous variable name: `l`
while l <= r:
mid = l + (r - l) // 2
if arr[mid] < x:
l = mid + 1

Check failure on line 7 in binary_search.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E741)

binary_search.py:7:13: E741 Ambiguous variable name: `l`
elif arr[mid] > x:
r = mid - 1
else:
return mid # Found
return -1 # Not found


# Example usage
arr = [int(x) for x in input().split()]
x = int(input())
result = binary_search(arr, x)
if result == -1:
print("Not found")
else:
print(f"Found at index {result}")
Loading