Back to course

Lesson 7: Analyzing Linear Time: O(N)

Algorithms: From Zero to Hero (A Beginner's Guide)

7. Analyzing Linear Time: O(N)

An algorithm runs in Linear Time, O(N), if the execution time grows directly and proportionally with the size of the input (N).

If the input doubles, the execution time approximately doubles.

Example: Simple Iteration (Searching an array)

If you want to find an item in a list (without knowing its position), in the worst case, you might have to look at every single element.

python def find_item(data, target): # The loop runs N times, where N is the length of 'data' for item in data: if item == target: return True return False

If N=100, the loop runs 100 times. If N=1000, it runs 1000 times. This direct relationship makes it O(N).