Programming & CS · Beginner–Pro · EGE

Big-O Time Complexity & Binary Search

Understand why O(1) hash lookup and O(log n) binary search beat nested O(n²) loops.

Rule & Structure

Binary search requires a SORTED array and halves the search interval every step, achieving O(log₂ n) time — finding an element in 1,000,000 items takes at most 20 comparisons.

ComplexityAlgorithm / OperationSteps for n = 1,000
O(1)Hash table / Python dict lookup1
O(log n)Binary search (sorted array)~10
O(n)Linear scan through list1,000
O(n log n)Merge sort / Timsort (sorted())~10,000
O(n²)Nested loops / Bubble sort1,000,000

Real Examples

mid = (left + right) // 2

Middle index in Python binary search

Common Mistakes

✗ Avoid: Running binary search on an unsorted list
✓ Correct: Sort first or use a hash set / dict for O(1) lookup

Binary search gives wrong answers if order is not monotonic.

Check your knowledge

What is the average time complexity of looking up a key in a Python dict (hash map)?

Practice this topic with AI Tutor

FlyTalkAI checks your real sentences, explains mistakes, and creates adaptive follow-up drills for this exact topic.

Continue learning in FlyTalkAI