Linear Search

Find an element by checking each position sequentially

Algorithm Code

Time: O(n) | Space: O(1)

1function linearSearch(arr, target) {
2  for (let i = 0; i < arr.length; i++) {
3    if (arr[i] === target) {
4      return i; // Found! Return the index
5    }
6  }
7  return -1; // Not found
8}

Algorithm Steps

Step 1 of 6

1

Initialize Linear Search

Search for target 22 in the array

2

Check Index 0

Compare arr[0] = 64 with target 22

3

Check Index 1

Compare arr[1] = 25 with target 22

4

Check Index 2

Compare arr[2] = 12 with target 22

5

Check Index 3

Compare arr[3] = 22 with target 22

6

Target Found!

Found target 22 at index 3

Target: 22

Array

0
64
1
25
2
12
3
22
4
11
5
90
Step 0 of 50%
0.25x4x