Linear Search Formula

The Formula

O(n) time complexity

When to use: Looking for your keys by checking every pocket and drawer in order.

Quick Example

Find 37 in [56,12,37,5,23]: check 56 (no), 12 (no), 37 (yes) โ€” found in 3 steps.

What This Formula Means

A search algorithm that checks each element in a list one by one, starting from the first, until the target is found or the entire list has been examined. It is the simplest search algorithm and works on both sorted and unsorted data.

Looking for your keys by checking every pocket and drawer in order.

Formal View

Linear search iterates through A[0], A[1], \ldots, A[n-1], returning the first index i where A[i] = t. Worst case: n comparisons (O(n)). Best case: 1 comparison (O(1)). Average: n/2 comparisons.

Common Mistakes

  • Using linear search on large sorted datasets when binary search would be dramatically faster
  • Forgetting to handle the case where the element is not found, leading to index errors
  • Stopping at the first match when you need to find all occurrences of the target value

Why This Formula Matters

Linear search works on any list regardless of order, making it the most universally applicable search algorithm. It serves as the baseline against which more efficient algorithms like binary search are measured.

Frequently Asked Questions

What is the Linear Search formula?

A search algorithm that checks each element in a list one by one, starting from the first, until the target is found or the entire list has been examined. It is the simplest search algorithm and works on both sorted and unsorted data.

How do you use the Linear Search formula?

Looking for your keys by checking every pocket and drawer in order.

Why is the Linear Search formula important in CS Thinking?

Linear search works on any list regardless of order, making it the most universally applicable search algorithm. It serves as the baseline against which more efficient algorithms like binary search are measured.

What do students get wrong about Linear Search?

Linear search works on any list; binary search is faster but requires sorted data.

What should I learn before the Linear Search formula?

Before studying the Linear Search formula, you should understand: searching, array.