The Top 3 Most Popular Search Algorithms

Kapil Khatik
4 min readJun 8, 2024

--

1. Linear Search

Overview

Linear search, also known as sequential search, is the simplest search algorithm. It works by checking each element of the list until the desired element is found or the list ends.

Example

Think about looking for a specific book on a shelf. You start from one end of the shelf and check each book until you find the one you want.

Steps

  1. Start at the first item in the list.
  2. Check if it is the item you’re looking for.
  3. If it is, you’re done!
  4. If it’s not, move to the next item and repeat.

Code Implementation

Iterative Approach:

def linear_search(arr, target):
for i in range(len(arr)):
if arr[i] == target:
return i
return -1

# Example usage
arr = [4, 2, 5, 1, 3]
target = 5
print(linear_search(arr, target)) # Output: 2

Recursive Approach:

def linear_search_recursive(arr, target, index=0):
if index >= len(arr):
return -1
if arr[index] == target:
return index
return linear_search_recursive(arr, target, index + 1)

# Example usage
arr = [4, 2, 5, 1, 3]
target = 5
print(linear_search_recursive(arr, target)) # Output: 2

Time Complexity

  • Best Case: O(1) (when the target is the first element)
  • Average Case: O(n)
  • Worst Case: O(n) (when the target is not in the list or the last element)

Space Complexity

  • Iterative Approach: O(1) (when the target is the first element)
  • Recursive Approach: O(n) (due to the call stack)

2. Binary Search

Overview

Binary search is an efficient algorithm for finding an element in a sorted list. It works by repeatedly dividing the search interval in half.

Example

Imagine you are looking for a word in a dictionary. Instead of starting from the first page and going page by page, you open the book roughly in the middle, check if the word is there, and decide whether to look in the first or second half of the book based on where the word should be alphabetically.

Steps

  1. Look at the middle item of the list.
  2. If it’s the item you want, you’re done!
  3. If it’s not, decide if the item you’re looking for is smaller or larger.
  4. If it’s smaller, repeat the process with the left half of the list.
  5. If it’s larger, repeat the process with the right half of the list.

Code Implementation

Iterative Approach:

def binary_search(arr, target):
left, right = 0, len(arr) - 1
while left <= right:
mid = left + (right - left) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1

# Example usage
arr = [1, 2, 3, 4, 5]
target = 3
print(binary_search(arr, target)) # Output: 2

Recursive Approach:

def binary_search_recursive(arr, target, left=0, right=None):
if right is None:
right = len(arr) - 1
if left <= right:
mid = left + (right - left) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
return binary_search_recursive(arr, target, mid + 1, right)
else:
return binary_search_recursive(arr, target, left, mid - 1)
return -1

# Example usage
arr = [1, 2, 3, 4, 5]
target = 3
print(binary_search_recursive(arr, target)) # Output: 2

Time Complexity

  • Best Case: O(1) (when the target is the first element)
  • Average Case: O(log n)
  • Worst Case: O(log n)

Space Complexity

  • Iterative Approach: O(1) (when the target is the first element)
  • Recursive Approach: O(log n) (due to the call stack)

3. Depth-First Search (DFS)

Overview

Depth-First Search (DFS) is an algorithm for traversing or searching tree or graph data structures. It starts at the root (or an arbitrary node for a graph) and explores as far as possible along each branch before backtracking.

Example

Imagine exploring a maze. You start at the entrance and keep going down each path until you reach a dead end. Then, you backtrack and try the next path.

Steps

  1. Start at the beginning point.
  2. Move to an adjacent point (like following a path in the maze).
  3. Continue this process until you reach a dead end.
  4. When you reach a dead end, go back to the last point where you had other paths to explore.
  5. Repeat until all possible paths have been explored.

Code Implementation

Iterative Approach:

def dfs_iterative(graph, start):
visited = set()
stack = [start]
while stack:
vertex = stack.pop()
if vertex not in visited:
visited.add(vertex)
stack.extend(set(graph[vertex]) - visited)
return visited

# Example usage
graph = {
'A': ['B', 'C'],
'B': ['A', 'D', 'E'],
'C': ['A', 'F'],
'D': ['B'],
'E': ['B', 'F'],
'F': ['C', 'E']
}
print(dfs_iterative(graph, 'A')) # Output: {'A', 'B', 'D', 'E', 'F', 'C'}

Recursive Approach:

def dfs_recursive(graph, start, visited=None):
if visited is None:
visited = set()
visited.add(start)
for neighbor in graph[start]:
if neighbor not in visited:
dfs_recursive(graph, neighbor, visited)
return visited

# Example usage
graph = {
'A': ['B', 'C'],
'B': ['A', 'D', 'E'],
'C': ['A', 'F'],
'D': ['B'],
'E': ['B', 'F'],
'F': ['C', 'E']
}
print(dfs_recursive(graph, 'A')) # Output: {'A', 'B', 'D', 'E', 'F', 'C'}

Time Complexity

  • Best Case, Average Case, Worst Case: O(V + E) (where V is the number of vertices and E is the number of edges)

Space Complexity

  • Iterative Approach: O(V)
  • Recursive Approach: O(V) (due to the call stack)

Conclusion

These search algorithms are like different strategies for finding things:

  • Linear Search is like checking every book on a shelf one by one.
  • Binary Search is like quickly finding a word in a dictionary by looking in the middle and deciding which half to search next.
  • Depth-First Search is like exploring every path in a maze until you find your way out.

By understanding these simple strategies, you can get a sense of how computers efficiently find and retrieve information.

--

--

Kapil Khatik

Passionate about Python & Machine Learning | Fostering innovation in the AI space.