ArticleslgStudy

computer science

Time complexity

Time complexity is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Time complexity rather than just read about it. In short: In theoretical computer science, the time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations performed by the algorithm, supposing that each elementary operation takes a fixed amount of time to perform.

Time complexity — main illustration
Time complexity — illustration

Key takeaways

  • Time complexity belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Time complexity to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Time complexity from memory before moving on to harder problems.

Reference excerpt

In theoretical computer science, the time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations performed by the algorithm, supposing that each elementary operation takes a fixed amount of time to perform. Thus, the amount of time taken and the number of elementary operations performed by the algorithm are taken to be related by a constant factor. Since an algorithm's running time may vary among different inputs of the same size, one commonly considers the worst-case time complexity, which is the maximum amount of time required for inputs of a given size. Less common, and usually specified explicitly, is the average-case complexity, which is the average of the time taken on inputs of a given size (this makes sense because there are only a finite number of possible inputs of a given size). In both cases, the time complexity is generally expressed as a function of the size of the input. Since this function is generally difficult to compute exactly, and the running time for small inputs is usually not consequential, one commonly focuses on the behavior of the complexity when the input size increases—that is, the asymptotic behavior of the complexity. Therefore, the time complexity is commonly expressed using big O notation, typically O(n), O(nlog⁡n), O(nα), O(2n), etc., where n is the size in units of bits needed to represent the input. Algorithmic complexities are classified according to the type of function appearing in the big O notation. For example, an algorithm with time complexity O(n) is a linear time algorithm and an algorithm with time complexity O(nα) for some constant α>0 is a polynomial time algorithm.

Table of common time complexities

The following table summarizes some classes of commonly encountered time complexities. In the table, poly(x)=xO(1), i.e., polynomial in x.

An algorithm is termed constant time (often represented as O(1) time) when its complexity function T(n) is bounded by a value that does not change with the size of the input. This implies that the execution time remains consistent regardless of how much data is processed. For instance, accessing a specific element in an array is a constant-time operation, as it requires only a single operation to locate that element. In contrast, determining the minimum value in an unordered array is not constant time; it requires examining each element, resulting in linear time complexity, or O(n). However, if the number of elements is known and fixed, certain tasks can still be considered constant time. Importantly, the term "constant time" does not mean the running time must be entirely independent of the problem size; rather, it should have a consistent upper bound irrespective of input size. For example, a task that involves swapping the values of a and b to ensure a≤b is classified as constant time, even if the execution time may vary depending on whether the condition is already met. The key is that there exists a constant t such that the time taken will never exceed t, regardless of the input values. Constant-time algorithms are especially significant in contexts like cryptography, where timing attacks can exploit variations in execution time. By designing algorithms that run in constant time, developers can enhance security and ensure performance predictability, making it a fundamental consideration in software engineering.

Logarithmic time

An algorithm is said to take logarithmic time when T(n)=O(log⁡n). Since logan and logbn are related by a constant multiplier, and such a multiplier is irrelevant to big O classification, the standard usage for logarithmic-time algorithms is O(log⁡n) regardless of the base of the logarithm appearing in the expression of T. Algorithms taking logarithmic time are commonly found in operations on binary trees or when using binary search. An O(log⁡n) algorithm is considered highly efficient, as the ratio of the number of operations to the size of the input decreases and tends to zero when n increases. An algorithm that must access all elements of its input cannot take logarithmic time, as the time taken for reading an input of size n is of the order of n. An example of logarithmic time is given by dictionary search. Consider a dictionary D which contains n entries, sorted in alphabetical order. We suppose that, for 1≤k≤n, one may access the k-th entry of the dictionary in a constant time. Let D(k) denote this k-th entry. Under these hypotheses, the test to see if a word w is in the dictionary may be done in logarithmic time: consider D(⌊n2⌋), where ⌊⌋ denotes the floor function. If w=D(⌊n2⌋)--that is to say, the word w is exactly in the middle of the dictionary--then we are done. Else, if w<D(⌊n2⌋)--i.e., if the word w comes earlier in alphabetical order than the middle word of the whole dictionary--we continue the search in the same way in the left (i.e. earlier) half of the dictionary, and then again repeatedly until the correct word is found. Otherwise, if it comes after the middle word, continue similarly with the right half of the dictionary. This algorithm is similar to the method often used to find an entry in a paper dictionary. As a result, the search space within the dictionary decreases as the algorithm gets closer to the target word.

Polylogarithmic time An algorithm is said to run in polylogarithmic time if its time T(n) is O((log⁡n)k) for some constant k. Another way to write this is O(logkn). For example, matrix chain ordering can be solved in polylogarithmic time on a parallel random-access machine, and a graph can be determined to be planar in a fully dynamic way in O(log3n) time per insert/delete operation.

Sub-linear time An algorithm is said to run in sub-linear time (often spelled sublinear time) if T(n)=o(n). In particular this includes algorithms with the time complexities defined above. The specific term sublinear time algorithm commonly refers to randomized algorithms that sample a small fraction of their inputs and process them efficiently to approximately infer properties of the entire instance. This type of sublinear time algorithm is closely related to property testing and statistics. Other settings where algorithms can run in sublinear time include:

… excerpt ends here. Continue reading the full article.

Illustrations

Time complexity: Graphs of functions commonly used in the analysis of algorithms, showing the number of operations N as the result of input size n for each function
Graphs of functions commonly used in the analysis of algorithms, showing the number of operations N as the result of input size n for each function

Worked examples

Example 1 — a first encounter with Time complexity

Start with the simplest possible case. Write down what Time complexity claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Time complexity before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Time complexity ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Time complexity

In research
Time complexity appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Time complexity in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Time complexity is common in secondary-school and first-year university syllabi. It links to neighbouring topics Analysis of algorithms, Computational complexity theory, Computational resources, so understanding it makes those chapters shorter.
In everyday life
Look for Time complexity outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Time complexity” →

How to study Time complexity in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Time complexity means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Time complexity out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Time complexity in simple terms?

In theoretical computer science, the time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations performed by the algorithm, supposing that each element…

Why does Time complexity matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Time complexity?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Time complexity.

Tags

  • Analysis of algorithms
  • Computational complexity theory
  • Computational resources
  • Time

Keep exploring