lesson

Updated 6 days ago ยท 1 view
Imagine rummaging through an unsorted bag of keys to find the one that unlocks your front door. You grab one key at a time, test it, and keep checking until you either find the right one or run out of keys.
This exact approach is the linear search algorithm (also called sequential search) โ checking each element of a list one by one in order until a match is found.
๐Interactive diagram
How do we write down this sequential logic clearly before translating it into code?
Algorithm Logic and Pseudocode
We start an index pointer at 0 and compare the item at that position to our target value. An iteration is one complete pass through a loop.
If the current element matches the target, we return the index immediately. If the loop completes without a match, we return
-1 to indicate the element is absent.๐Interactive diagram
Now, how does this logic look when implemented in standard JavaScript?
JavaScript Implementation
In JavaScript, a standard
for loop traverses the array by index, returning as soon as a match is detected.๐Interactive diagram
If our list grows from ten items to ten million items, how much longer will the search take?