What I Learned About Binary Search Today

Today, I explored Binary Search, an efficient way to search for an item in a sorted list. Unlike linear search which goes one by one, binary search cuts the list in half each time — which makes it really fast.
Here’s how Binary Search works:
Start with the full list.
Check the middle number.
If it’s what you’re looking for — boom! You're done.
If not, decide if your number is smaller or larger and throw away half the list.
Keep repeating until you find the number or run out of options.
It’s like searching for a word in the dictionary — you don’t start from the first page; you flip to the middle and narrow it down.
✅ My Binary Search Code in Python:
def binary_search(arr,target):
n = len(arr)
low = 0
high = n-1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid
if arr[mid] < target:
low = mid + 1
if arr[mid] > target:
high = mid -1
return f"item {target} not found"
arr = [1, 3, 5, 8, 13, 21, 34]
print(binary_search(arr,21))
Binary Search is super useful when working with big sorted data — and I’m glad to now understand how it works from scratch!
Subscribe to my newsletter
Read articles from kasumbi phil directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
