Searching & Sorting AlgorithmsBeginner LevelStudy Time: 10 mins

Linear Search Algorithm

Linear Search scans elements sequentially one-by-one from the start of an array to the end until the target item is found or the array ends.

Loading visualizer workspace...

Linear Search Execution Trace

Sample Input:Array: [14, 28, 42, 56, 70], Target: 42
Step 1

Check Index 0

Compare arr[0] (14) with target 42. 14 != 42 -> Move to next index.

arr[0] = 14 != 42 -> Increment index to 1
Pointers advance sequentially one element at a time.
Step 2

Check Index 1

Compare arr[1] (28) with target 42. 28 != 42 -> Move to next index.

arr[1] = 28 != 42 -> Increment index to 2
Continues scanning unsorted element slots.
Step 3

Target Match at Index 2

Compare arr[2] (42) with target 42. 42 == 42 -> Target found!

arr[2] = 42 == 42 -> Return Index 2
Search terminates successfully upon first target match.

Real-world analogy: Searching Lost Keys

Sequential
Hover over items for details

Looking for keys in a messy drawer by checking every single pocket one by one from top to bottom.

  • Sequential= Checks elements in exact linear order.

Complexity analysis

Scenario Simulator: Dataset Size (N = 500)
Presets:
N = 10N = 10,000
Complexity Curve MapLinear Search: O(N) Linear Time
Time (t)Size N (500)N = 500O(N²)O(N log N)O(N)O(log N)O(1)Linear Search Li
Time Steps (N=500)

500 steps (Linear)

O(N) Linear Time

Space Footprint

O(1) / O(N) Space

Memory growth rate as N expands.

Performance Rank
Efficient Tier

Relative efficiency rating for large N.

OperationTime ComplexitySpace Complexity
Best Case (First Item)O(1)O(1)
Average CaseO(N)O(1)
Worst Case (Last / Absent)O(N)O(1)

Code implementations

function linearSearch(arr, target) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === target) return i;
  }
  return -1;
}

Real-world applications

Case 01

Unsorted Lists

Scans raw unsorted user input streams.

Case 02

Small Datasets

Extremely fast for small arrays under 15 elements due to CPU cache locality.

Interview questions

Topic Knowledge Test & Assessment

10 Questions Assessment

Linear Search Knowledge & Skill Test

Validate your conceptual understanding, operation mechanics, and core concepts of Linear Search.