Linear Search Visualizer
365
365education
Linear Search
Interactive Visualizer & Guide
Interactive Visualizer
Linear Search Implementation
How Linear Search Works
Linear Search (also known as sequential search) is the simplest searching algorithm. It inspects every element in a list sequentially from start to finish until the target element is found or the end of the collection is reached.
Unlike Binary Search , Linear Search does not require the array to be sorted . It can be performed directly on unsorted arrays, linked lists, or streaming data.
Time & Space Complexity
| Case | Complexity | Notes |
|---|---|---|
| Best Case | O(1) | Target is at the first index. |
| Average Case | O(n) | Target is roughly in the middle. |
| Worst Case | O(n) | Target is last or missing. |
| Space Complexity | O(1) | Iterative implementation uses constant auxiliary space. |
Step by Step Execution
- Start at index 0: Set the loop counter to the first index.
-
Compare current element:
Check whether
a[i] == target. -
If a match is found:
Return index
i. -
If no match:
Increment
i = i + 1. - Repeat: Continue until the array boundary is reached.
-
End of array:
Return
-1if the target is missing.
⚖ When to Use Linear Search
✔ Use it when:
- The array is small or unsorted.
- Performing a single search.
- Accessing sequential data such as linked lists.
✖ Consider alternatives when:
- The dataset is large and sorted.
- Many repeated searches are required.
✓ Worked Example
Searching for
59
in
[11, 6, 4, 98, 59, 79]
Index 0: 11 ≠ 59
Continue
Index 1: 6 ≠ 59
Continue
Index 2: 4 ≠ 59
Continue
Index 3: 98 ≠ 59
Continue
Index 4: 59 == 59
MATCH FOUND!
