Back to Visualizer
Linear Search
Linear Search is the simplest searching algorithm that checks every element sequentially until the target is found or the list ends.
Best: O(1)Worst: O(n)
Size:20
Speed:
Unchecked
Checking
Checked
Found
0
Comparisons Made
How It Works
- 1Start from the first element of the array
- 2Compare current element with target value
- 3If match found, return the index
- 4If not, move to the next element
- 5Repeat until element found or array ends
- 6Return -1 if element not in array
Time Complexity
Best CaseO(1)
Average CaseO(n)
Worst CaseO(n)
Space ComplexityO(1)
Code
function linearSearch(arr, target) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === target) {
return i; // Found at index i
}
}
return -1; // Not found
}