CampusFlow
AlgorithmsSearching Algorithms

🔍 Searching Algorithms

Find elements efficiently in data structures. From simple linear search to logarithmic binary search, understand the techniques that power search engines and databases.

Overview

ℹ️

What are Searching Algorithms?

Searching algorithms locate specific elements in data structures. The efficiency depends on whether data is sorted and how much space you can use. Binary search is the gold standard for sorted data.

Key Searching Algorithms

Linear Search

Check each element sequentially until found. Works on unsorted arrays.

Time: O(n)Space: O(1)

Binary Search

Efficiently search sorted arrays by eliminating half the search space each iteration

Time: O(log n)Space: O(1)

Jump Search

Jump ahead in blocks, then linear search within a block

Time: O(√n)Space: O(1)

Interactive Visualizer

Algorithm Visualization

🔍

Linear Search Visualizer

Watch how the search traverses the array, highlighting comparisons and narrowing search space.

Deep Dive: Linear Search

Time Complexity

Time: O(n)

Space Complexity

Space: O(1)

Prerequisite

Any Array
💡

Algorithm Approach

Check each element sequentially until found. Works on unsorted arrays.
1

Initialize

Set up initial search boundaries or position based on algorithm requirements.

2

Compare

Check current position(s) against target value.

3

Narrow Search

Use comparison result to eliminate parts of search space or move to next position.

4

Repeat

Continue until target is found or search space is exhausted.

python

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

Algorithm Comparison

AlgorithmTimeSpaceRequires Sorted?Best For
Linear SearchO(n)O(1)NoSmall/unsorted data
Binary SearchO(log n)O(1)YesMost sorted arrays
Jump SearchO(√n)O(1)YesLarge sorted arrays

Interview Questions

Why is binary search O(log n)?

Binary search eliminates half the search space with each comparison. After k steps, search space is n/2^k. When this equals 1, we've found the answer: n/2^k = 1 means k = log₂(n).

Can binary search work on unsorted arrays?

No, binary search relies on the sorted property to make decisions about which half to eliminate. On unsorted data, those decisions are meaningless. Use linear search instead.

What's the difference between searching and sorting?

Searching finds an element; sorting arranges all elements. Sorting is often a preprocessing step for efficient searching (binary search requires sorted data). Sorting is O(n log n) minimum, but enables O(log n) searches.

When would you use linear search over binary search?

1) Data is unsorted and sorting cost is prohibitive, 2) Array is very small (linear search has better constants), 3) Only searching once (binary search cost not amortized), 4) Need to find all occurrences.

Common Mistakes to Avoid

⚠️

Using binary search on unsorted arrays - always verify array is sorted first

⚠️

Off-by-one errors in binary search - be careful with mid calculation and boundary updates

⚠️

Not handling edge cases: empty array, single element, element not found

⚠️

Forgetting that binary search requires O(log n) space with recursion - use iteration for O(1) space

⚠️

Comparing binary search with data structures like hash tables that can search in O(1) average

What's Next?

Explore Related Topics

Build on searching by learning: