algoritmauzmanı
TREN
1Introduction
Lesson 1.3

A short look at Big-O

When you compare two data structures you cannot answer "which one is faster?" in seconds; the time depends on the computer, the language and whatever else is running. Instead you look at how many steps an operation takes and how that number grows as the data grows. The short way of writing that growth is called notation.

What you will learn
  • That Big-O measures how the number of steps grows, not the running time
  • The difference between , , and , and why constants are dropped
  • The worst case, the difference between time and space complexity, and the complexity of linked list operations
8 min

1Growth, not time

The of an operation expresses how many steps it takes when the data has size n. A step is a small piece of work that finishes in constant time, such as an assignment, a comparison or moving a pointer on by one node.

Big-O does not give the exact number of steps; it tells you how that number grows as n grows. For example, when you search for a value in a list of n elements you look at each element once. The number of steps is proportional to n; we write this as O(n).

2Four basic growth rates

  • , constant: the step count does not depend on n. Accessing an array by index, or inserting at the head of a linked list.
  • , logarithmic: the remaining work halves at every step. Binary search in a sorted array.
  • , linear: each element is looked at once. Searching a list or printing every element.
  • , quadratic: for each element, every element is looked at. Two nested loops.

You see how big the gap is when you look at the step counts:

Complexityn = 10n = 1,000n = 1,000,000
O(1)111
O(log n)about 3about 10about 20
O(n)101,0001,000,000
O(n²)1001,000,0001,000,000,000,000

For small n they are all fast. With a million elements, though, O(n²) means a trillion steps; even on a computer doing a billion steps per second that takes about 17 minutes. The same job in O(n) is done in a thousandth of a second.

3Constants and smaller terms are dropped

If a loop does three things for each element and there are five setup steps at the start, the total is 3n + 5 steps. In Big-O this is written O(n). The reason: as n grows, the factor 3 and the extra 5 do not change the shape of the growth; double the list and the time still roughly doubles.

For the same reason an operation taking n² + n steps is O(n²). When n is a million, n² is a trillion while n is only a million; the smaller term's effect disappears.

Dropping constants does not mean two O(n) operations finish in the same time. Big-O only compares how they behave as the data grows.

4The worst case

The same operation can take different amounts of time on two inputs of the same size. When searching a list, if the value is in the first node you find it in one step; if it is not in the list at all you look at all n nodes. Unless stated otherwise, complexity is given for the . That way you know the operation never goes beyond that bound on any input.

5Time and space

Big-O is not only used for time. How much extra memory an operation needs beyond its input is called , and it is written the same way. Inserting at the head allocates space only for the new node, so its space complexity is O(1). An operation that makes a copy of the list has space complexity O(n).

6Big-O in linked list operations

Every linked list lesson has a complexity section. Here is a summary ahead of time:

OperationTimeWhy
Insert at the headO(1)One allocation and two assignments. The length of the list does not matter.
Insert at the end (no )O(n)You have to the whole list to find the last node.
Insert at the end (with tail)O(1)The last node is already known.
Search for a valueO(n)In the worst case every node is looked at once.

If we wrote the same table for an array, access by index would be O(1) and inserting at the front would be O(n). This is the idea from What is a data structure?, that no structure is best at everything, put into numbers.

7Try it

An operation takes 5n + 20 steps. How is it written in Big-O?

Show the answer

The answer is B. The constant factor 5 and the added 20 do not change the shape of the growth as n gets larger. The step count grows in direct proportion to n, so it is .

Why is inserting at the end of a singly linked list O(n) when no tail pointer is kept?

Show the answer

The answer is B. Nodes in a linked list are never shifted; the insertion itself is a few assignments. The cost comes from finding the last node: you start at and pass n nodes. If a is kept, the operation becomes .

Lessons you mark show up in the curriculum. This is kept on this device only.