Searching

Problem shape

Beginner
4
8
15
16
23
42
Core idea

Scan each element until the target is found or the list ends.

Step 1 / 5
Problem shape
Current lesson

Linear Search Lab

Searching Beginner

Linear Search

Welcome to the Linear Search algorithm lab. Scan each element until the target is found or the list ends. Use it when: Use on unsorted data or tiny collections where setup cost is not worth it. The time complexity is O(n), and the space complexity is O(1). The pseudocode is: for i from 0 to n - 1 if a[i] == target return i return -1 As you practice, track the state that changes and the condition that makes the algorithm stop.

Time

O(n)

Space

O(1)

Pseudocode

for i from 0 to n - 1
  if a[i] == target
    return i
return -1
Open deeper structure lesson
1.0x