Searching Algorithms Cheat Sheet
Covers linear and binary search, graph traversal with BFS and DFS, and guidance on choosing the right search strategy.
1 PageIntermediateApr 15, 2026
Search Strategies
Common approaches to finding data, ordered by typical use case.
- Linear Search- O(n) time, O(1) space; checks each element sequentially, works on unsorted data
- Binary Search- O(log n) time, O(1) space; requires sorted data, repeatedly halves the search range
- Breadth-First Search (BFS)- O(V + E) time; explores a graph level by level using a queue, finds shortest paths in unweighted graphs
- Depth-First Search (DFS)- O(V + E) time; explores as far as possible along each branch using a stack or recursion
- Hash-based Lookup- Average O(1) time via a hash table or set; no ordering requirement but uses extra memory
Linear vs Binary Search
Two fundamental search algorithms compared.
python
# Linear search: O(n)def linear_search(arr, target): for i, val in enumerate(arr): if val == target: return i return -1# Binary search: O(log n), requires sorted inputdef binary_search(arr, target): lo, hi = 0, len(arr) - 1 while lo <= hi: mid = (lo + hi) // 2 if arr[mid] == target: return mid elif arr[mid] < target: lo = mid + 1 else: hi = mid - 1 return -1import bisectbisect.bisect_left([1, 3, 5, 7], 5) # => 2, built-in binary search
BFS & DFS Graph Search
Traversing a graph represented as an adjacency list.
python
from collections import dequedef bfs(graph, start): visited = {start} queue = deque([start]) order = [] while queue: node = queue.popleft() order.append(node) for neighbor in graph[node]: if neighbor not in visited: visited.add(neighbor) queue.append(neighbor) return orderdef dfs(graph, start, visited=None, order=None): if visited is None: visited, order = set(), [] visited.add(start) order.append(start) for neighbor in graph[start]: if neighbor not in visited: dfs(graph, neighbor, visited, order) return order
Choosing a Strategy
Matching the search algorithm to the problem.
- Unsorted, small data- Linear search; the overhead of sorting first isn't worth it for one-off lookups
- Sorted data, many queries- Binary search or a sorted structure amortizes the O(n log n) sort cost across queries
- Shortest path, unweighted graph- BFS guarantees the fewest edges to reach a target
- Exploring all paths- DFS is simpler to implement recursively and uses less memory than BFS for deep graphs
- Frequent exact-match lookups- A hash set or map beats both linear and binary search with O(1) average time
Pro Tip
Binary search bugs almost always come from the loop bounds or midpoint update — use lo <= hi with lo = mid + 1 / hi = mid - 1, and prefer mid = lo + (hi - lo) // 2 to avoid overflow in fixed-width integer languages.
Was this cheat sheet helpful?
Explore Topics
#SearchingAlgorithms#SearchingAlgorithmsCheatSheet#Programming#Intermediate#SearchStrategies#LinearVsBinarySearch#BFS#DFS#DataStructures#Algorithms#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance