Computer ScienceAlgorithmsMedium

Time Complexity

Also known as:Running timeComputational timeExecution complexity

Time complexity is a measure of the amount of time an algorithm takes to complete as a function of the size of its input, typically expressed using Big O notation. It describes how the running time grows relative to the input size, helping developers predict performance and compare algorithms. Understanding time complexity is essential for writing scalable software, especially when dealing with large datasets.

Key Formula

T(n) = O(f(n))

LaTeX: T(n) = O(f(n))

SymbolMeaningUnit
T(n)Running time of the algorithmoperations
nSize of the inputelements
f(n)Mathematical function bounding the growth ratedimensionless

Worked Example

Problem

An algorithm iterates through an array of n=100 elements once, then iterates through it again in a nested loop. What is the time complexity?

Solution

Step 1: The first single loop runs n times → O(n). Step 2: The nested loop runs n × n times → O(n²). Step 3: Total time = O(n) + O(n²). Step 4: Drop the lower-order term: the dominant term is O(n²).

Answer

O(n²) — quadratic time complexity.

Common Time Complexity Classes

NotationNameExample Algorithmn=10 opsn=100 ops
O(1)ConstantArray index lookup11
O(log n)LogarithmicBinary search37
O(n)LinearLinear search10100
O(n log n)LinearithmicMerge sort33664
O(n²)QuadraticBubble sort10010,000
O(2ⁿ)ExponentialRecursive Fibonacci1,0241.27 × 10³⁰

Interactive Tools

Big-O Cheat Sheet

Visual reference for algorithm complexities

Open Tool

Desmos

Graph and compare growth functions

Open Tool

Wolfram Alpha

Compute and compare mathematical functions

Open Tool
Graph comparing common time complexity growth functions

Wikimedia Commons, CC BY-SA

Related Terms

The term "time complexity" emerged from computational complexity theory, a branch of theoretical computer science formalized in the 1960s. Key contributions came from Juris Hartmanis and Richard Stearns, who introduced the notion of computational complexity in their 1965 paper.

time-complexitybig-oalgorithmsperformancescalabilitycomputer-science