Linear Search

Searching

Linear Search

▾

64

34

25

12

22

11

90

0

1

2

3

4

5

6

Starting Linear Search for target value 12.

Presets:

Size:

Timeline Progress:

0%

Step 1 / 6

Comparisons

0

Swaps / Actions

0

Speed:

Shortcuts: Space Play • ←/→ Step • R Reset • M Sound • Z Focus

Implementation Code

1

2

3

4

5

6

7

8

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

Complexity Analysis

Best Case

O(1)

Average Case

O(n)

Worst Case

O(n)

Space

O(1)

Color Legend

Array Element

Comparing / Inspecting

Target Found

Linear Search sequentially checks each element in the list from left to right until the target value is found or the entire list has been traversed. It requires no prior sorting and works on any collection, but has linear O(n) time complexity in the average and worst cases.