The most fundamental search algorithm: checking every element in a sequence until the target is found or the end is reached.
Parameters
12
Visualization
Step: 0
Enter a target and click Step to start searching.
Algorithm Insight
Linear search examines each element in an array one by one in sequential order.
1.Start from the first element (index 0).
2.Compare current element with the Target.
3.If they match, search is successful!
4.Otherwise, move to the next element and repeat.
Efficiency
Time ComplexityO(N)
Space ComplexityO(1)
Deconstructing Linear Search
The simplest search of all: checking items one by one until you find what you're looking for.
Quick Context: What is Linear Search?
Linear Search, also known as sequential search, is the most straightforward search algorithm. It works by iterating through a collection of items—like an array—one by one, from the beginning to the end, until the target item is found. If the algorithm reaches the end of the collection without finding the item, it concludes that the item is not present. It's the digital equivalent of looking for a specific book on a messy shelf by checking every single book title.
The Core Idea: One-by-One Comparison
The logic of Linear Search is as simple as it gets. There's no complex setup or data requirement (like sorting).
Start at the Beginning: The search begins at the very first element (index 0) of the array.
Compare: The value of the current element is compared to the target value.
Match Found?:
If the values match, the search is successful! The algorithm stops and returns the index of the found element.
If they don't match, the algorithm moves to the next element in the sequence.
End of the Line: This process repeats until either a match is found or the end of the array is reached. If the end is reached, the target is not in the array.
Guided Experiments with the Visualizer
Set Up: Click "Randomize Array" to generate a new set of data. Enter a "Search Target" that you can see in the array.
First Step: Click "Next Step". A pointer appears at the first bar (index 0), highlighting it as the "active" element. The "Feedback Box" will tell you it's comparing this element's value to your target.
Continue Stepping: Keep clicking "Next Step". The pointer will move from left to right, one bar at a time. Each bar it leaves behind is marked as "checked" (faded out) to show it has been processed.
Finding the Target: When the pointer reaches the element that matches your target, the bar will turn bright green, and the search will stop. The "Feedback Box" will announce that the value has been found at that index.
Target Not Found: Now, enter a target value that is not in the array. Click "Auto-Run". Watch the pointer scan through every single element until it goes past the end. The "Feedback Box" will then report that the target was not found. This demonstrates the worst-case scenario.
Performance and When to Use It
Time Complexity: O(n) — In the worst case, the algorithm has to check every single one of the `n` elements in the array. This makes it inefficient for very large datasets compared to algorithms like Binary Search.
Space Complexity: O(1) — It requires no extra memory, as it operates directly on the input array.
Key Advantage: Its biggest strength is its simplicity and the fact that it works on any collection of data, whether it's sorted or not. Other, faster algorithms like Binary or Interpolation search require the data to be sorted first.
Key Takeaways
No Sorting Required: Works on unsorted data, which is a major advantage.
Simple to Implement: It's often the first search algorithm taught because its logic is so clear.
Predictable Performance: The time it takes is directly proportional to the size of the list.
The Brute-Force Method: It's a reliable but often slow "brute-force" approach to finding an item.
Cheat sheet
Linear Search
Linear Search, also known as sequential search, is the most straightforward search algorithm. It works by iterating through a collection of items—like an array—one by one, from the beginning to the end, until the target item is found. If the algorithm reaches the end of the collection without finding the item, it concludes that the item is not present. It's the digital equivalent of looking for a specific book on a messy shelf by checking every single book title.