Back to All Algorithms
Linear Search
Category: Searching | Time: O(n) | Space: O(1)
Visualization
Visual representation of the data structure
About Linear Search

A simple method for finding an element within a list. It sequentially checks each element of the list until a match is found or the whole list has been searched. It is one of the simplest searching algorithms.

Code Editor
The code is for reference. Editing it won't affect the visualization.
1
2
3
4
5
6
7
8
Variable State
{
  "arr": "[2, 8, 5, 12, 16, 23, 38, 56, 72, 91]",
  "target": 16
}
Linear Search: How It Works
Last updated October 28, 2026

Overview

Linear Search checks the elements one by one, from the first to the last, until it finds the target or runs out of elements. It makes no assumptions about the data: the array can be unsorted, contain duplicates, or hold values of any type that supports equality.

Because it may have to look at every element, its running time grows in direct proportion to the size of the array. That sounds slow, but for small arrays it is often the fastest option in practice, since it has no setup cost and walks memory sequentially, which CPUs handle very well.

In the default example the array holds ten values and the target is 16. The search compares 2, 8, 5 and 12 before finding 16 at index 4, so it returns 4 after five comparisons.

Step by step

  1. Start at index i = 0.
  2. If arr[i] equals the target, return i.
  3. Otherwise increment i.
  4. If i reaches the end of the array, return -1 to signal 'not found'.

Complexity

CaseCostWhy
Best caseO(1)The target is the first element.
Average caseO(n)On average about half the array is scanned.
Worst caseO(n)The target is last or missing.
Extra spaceO(1)Only the loop index.

When to use it

  • Unsorted data, where sorting first would cost more than the search itself.
  • Small arrays (tens of elements) and one-off lookups.
  • Linked lists and streams, where you cannot jump to the middle anyway.

Common mistakes

  • Repeated linear searches over the same large array add up quickly — build a hash set or sort once instead.
  • Returning inside the loop on the first mismatch is a classic bug that only checks the first element.
  • Comparing objects with === checks identity, not equal contents.
Worked Example: Linear Search Dry Run
Generated from the same trace that drives the visualizer above, using the default input.

A dry run is the best way to check your understanding of Linear Search: follow each row, predict the next one, and compare. The run below takes 12 steps in total. Steps that only re-print loop counters are left out so the decisions stand out. The Line column refers to the line numbers in the code listing above.

Input
2,8,5,12,16,23,38,56,72,91
Target
16
Result
return value = 4
StepLineArrayWhat happens
11[2, 8, 5, 12, 16, 23, 38, 56, 72, 91]target = 16
33[2, 8, 5, 12, 16, 23, 38, 56, 72, 91]i = 0, arr[i] = 2, target = 16, condition = 2 === 16
53[2, 8, 5, 12, 16, 23, 38, 56, 72, 91]i = 1, arr[i] = 8, target = 16, condition = 8 === 16
73[2, 8, 5, 12, 16, 23, 38, 56, 72, 91]i = 2, arr[i] = 5, target = 16, condition = 5 === 16
93[2, 8, 5, 12, 16, 23, 38, 56, 72, 91]i = 3, arr[i] = 12, target = 16, condition = 12 === 16
113[2, 8, 5, 12, 16, 23, 38, 56, 72, 91]i = 4, arr[i] = 16, target = 16, condition = 16 === 16
124[2, 8, 5, 12, 16, 23, 38, 56, 72, 91]return value = 4
Frequently Asked Questions
About Linear Search

What is Linear Search?

Linear Search is the most basic search algorithm. It sequentially checks each element of a list until a match is found or the whole list has been searched. It is simple to implement but often inefficient for large datasets.

What is the time complexity of Linear Search?

The time complexity is O(n) in the worst and average cases, as it may need to check every element. The best-case complexity is O(1), which occurs if the target element is the first one in the list.

What is the space complexity of Linear Search?

The space complexity is O(1) because it operates directly on the input list without requiring any additional memory proportional to the input size.

Does Linear Search require the array to be sorted?

No, a major advantage of Linear Search is that it does not require the data to be sorted. It can be used on any sequence of elements, regardless of their order.

How does Linear Search compare to Binary Search?

Linear Search is simpler but slower (O(n)) than Binary Search (O(log n)). However, Binary Search has a strict requirement that the array must be sorted, whereas Linear Search does not. For unsorted or very small arrays, Linear Search is often the more practical choice.

Can Linear Search be improved?

For unsorted data, it cannot be fundamentally improved. However, if the same elements are searched for frequently, you can use a technique like 'move-to-front', where a found item is moved to the beginning of the list to speed up subsequent searches for that same item.

Is Linear Search a good choice for linked lists?

Yes, Linear Search is the standard way to search a linked list, as linked lists do not support the random access required by more advanced algorithms like Binary Search. The time complexity remains O(n).

How do you handle a 'not found' scenario?

If the algorithm iterates through the entire list without finding the target element, it concludes that the element is not present. It typically signals this by returning a special value like -1, null, or false.

Can you implement Linear Search recursively?

Yes, it can be implemented recursively. A recursive function would check the current element, and if it's not a match, it would call itself on the rest of the list. However, the iterative approach is generally preferred as it is more space-efficient.

What is a sentinel Linear Search?

A sentinel Linear Search is a small optimization where the target element is added to the end of the list (as a 'sentinel'). This removes the need to check for the end of the array in each iteration of the loop, slightly reducing the number of comparisons per loop, although the overall complexity remains O(n).

Where can I learn more?

You can find a more detailed explanation on GeeksforGeeks.
Related Interview Questions
Practice with real problems