Linear Search
This algorithm is used for Strings and Lists often.
index()
andfind()
are linear searchesin
andnot in
membership for strings and lists are linear searches
Algorithm Classification
Big-O: O(n) hence the name “Linear” Search … happens when the target is not found -or- target is the last value
Big-Omega: O(1) very first item is the target
Big-Theta: O(n/2) which simplifies to O(n) … target is found somewhere in the middle
Linear Search Algorithm:
Note on using a while loop
Some of you may want to use a while loop with a counter rather than coding a for loop.
It is not recommended to do so because:
A while loop must check its condition on each iteration; whereas, a for loop has no looping condition to check at every iteration.
Last updated