LINEAR SEARCH

CHECK ONE BY ONE · O(n)


prostudypk.com
Search One by Onekey = 42 170✗81✗252✗33✗614✗95✗426✗307✗148✗59✗for i in range(n):    if a[i] == key:        return ireturn -1 Found! index 6 42target (key)current indexcomparisons
1. Linear SearchCheck every item from start to endFound → return the index, otherwise −1
2. SpeedBest case: 1 comparisonWorst case: n comparisons · O(n)
3. When to Use?When the list is unsortedWhen sorted → binary search
COMPUTER SCIENCE · ALGORITHMS · PUNJAB BOARDS
‹ Labs Next ›