Back to Home

Linear Search

A sequential search algorithm that starts at one end and goes through each element of a list until the desired element is found.

Visualization

Step 1 / 5
Speed:

Details

Analogy

It's like looking for your keys in a messy room. You have no system; you just check one spot, then the next, and so on, until you either find your keys or have checked every possible spot in the room.

Purpose

To find a target value within a list by checking each element one by one from the beginning to the end.

Use Cases

Useful for small or unsorted lists where more complex search algorithms don't offer a significant advantage. Its simplicity makes it easy to implement.

Algorithm Steps

Line 1
1
for i = 0 to n-1:
2
if arr[i] == target:
3
return i
4
return -1 (not found)
Current
Executed
Pending

Explanation

What's Happening?

Step 1/5

Start searching for target value 23. Check index 0: Is 12 equal to 23? No.

Updates with each step • Clickfor full view

Code Implementation

def linear_search(arr, target):
    for i in range(len(arr)):
        if arr[i] == target:
            return i  # Target found
    return -1  # Target not found
Linear Search - Interactive Visualization | AlgoViz | AlgoViz