Search algorithm and its complexity
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Search algorithms answer two different kinds of questions: "is this target in my data" and "how do I reach a goal state in a search space." The complexity depends not only on the algorithm but also on the data structure and problem model. That is why O(log n) binary search is excellent on sorted arrays yet useless on arbitrary graphs, while graph search algorithms solve pathfinding problems that array search cannot even express.
Start with the Simplest Data Search
Linear search checks each element until it finds the target or reaches the end.
Its complexity is:
- Best case:
O(1)if the first element matches. - Worst case:
O(n)if the element is absent or last.
Linear search works on unsorted data and is often the right answer for short lists where setup costs matter more than asymptotic analysis.
Use Binary Search Only on Sorted Data
Binary search repeatedly halves the search space, which is why it is much faster on large sorted collections.
Binary search has O(log n) worst-case time, but only if the data is sorted and random access is cheap. Using it on unsorted input breaks correctness, not just performance.
Search Graphs with BFS or DFS
Once the problem is a graph or tree rather than a flat array, the search question changes. Breadth-first search explores layer by layer and is useful for shortest paths in unweighted graphs. Depth-first search goes deep along one branch before backtracking.
Breadth-first search example:
For a graph with V vertices and E edges, both BFS and DFS run in O(V + E) time when implemented with adjacency lists.
Use Heuristics When Search Spaces Are Large
Some search problems are too large for blind exploration. A* search uses a heuristic estimate to guide exploration toward the goal.
Conceptually, A* prioritizes nodes by:
- Cost so far.
- Estimated remaining cost.
That makes it powerful for pathfinding, but the performance depends heavily on the heuristic quality. A poor heuristic reduces A* toward more expensive uninformed search behavior.
Even when the asymptotic worst case is still large, a good heuristic can make a practical difference of orders of magnitude.
Complexity Depends on the Representation
The phrase "search algorithm complexity" is incomplete unless you also state the data assumptions. Consider the difference:
- Array membership in unsorted data: linear search,
O(n). - Array membership in sorted data: binary search,
O(log n). - Hash table lookup: expected
O(1). - Graph reachability: BFS or DFS,
O(V + E).
The fastest search method often comes from choosing the right data structure before you even choose the algorithm.
Measure Space Complexity Too
Time complexity gets the attention, but memory matters too. DFS can use less memory than BFS on wide graphs because BFS stores an entire frontier. Binary search uses little extra space in its iterative form, while some recursive versions consume call stack depth. In real systems, memory pressure can make an algorithm with similar time complexity the better practical choice.
Pick the Algorithm That Matches the Question
Ask what the actual search problem is:
- Find a value in a small unsorted list: linear search is often fine.
- Search a sorted collection: binary search is the right baseline.
- Explore reachability or shortest paths in an unweighted graph: BFS.
- Traverse all paths or use backtracking: DFS.
- Goal-directed pathfinding with a good heuristic: A*.
Most search mistakes happen when an algorithm is chosen by name recognition rather than by problem shape.
Common Pitfalls
- Applying binary search to data that is not sorted.
- Discussing search complexity without mentioning the data structure assumptions.
- Using BFS where memory is too constrained for the frontier size.
- Assuming an asymptotically faster algorithm is always better for tiny inputs.
- Treating heuristic search as universally superior when the heuristic quality may be poor.
Summary
- Search algorithms solve different problems depending on whether the data is a list, tree, graph, or weighted search space.
- Linear search is simple and flexible but costs
O(n)in the worst case. - Binary search is
O(log n)but requires sorted, indexable data. - BFS and DFS are fundamental graph-search tools with
O(V + E)time on adjacency lists. - Complexity analysis only makes sense when paired with the right data assumptions and practical constraints.

