Linear scans vs logarithmic hunts
Beginner
Asymptotics & empirical timing
Created by Pavel
· 29.04.2026 at 19:08 UTC
Searching for a value in an unsorted Python list requires a linear scan in the worst case—you might inspect every element. If you can sort once and answer many queries, binary search (bisect) gives O(log n) per query after the O(n log n) sort. If you only need membership and not order, a hash set gives expected O(1) lookups at memory cost.
These trade-offs drive real pipelines: feature dictionaries keyed by string IDs, deduplication with sets, quantile thresholds on sorted arrays, and database indexes (B-trees) behind ORMs.
Choose the structure that matches your access pattern: one-off scan, many queries on static sorted data, or many membership tests with no ordering requirement.
bisect module: [1].
Sources
University approvals: 0
Tasks
Card Info
- Topic: Asymptotics & empirical timing
- Difficulty: Beginner
- Completed: 0 users
Creator
Pavel