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 , , , , etc., where 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 is a linear time algorithm and an algorithm with time complexity for some constant is a polynomial time algorithm.
Table of common time complexities
The following table summarizes some classes of commonly encountered time complexities. In the table, , i.e., polynomial in .
Name Complexity class Time complexity Examples of running times Example algorithms constant time 10 Finding the median value in a sorted array of numbers. Calculating . inverse Ackermann time Amortized time per operation using a disjoint set iterated logarithmic time Distributed coloring of cycles log-logarithmic Amortized time per operation using a bounded priority queue logarithmic time DLOGTIME , Binary search polylogarithmic time fractional power where , Range searching in a k-d tree linear time , Finding the smallest or largest item in an unsorted array. Kadane's algorithm. Linear search. "n log-star n" time Seidel's polygon triangulation algorithm. linearithmic time , Fastest possible comparison sort. Fast Fourier transform. quasilinear time Multipoint polynomial evaluation quadratic time Bubble sort. Insertion sort. Direct convolution cubic time Naive multiplication of two matrices. Calculating partial correlation. polynomial time P , Karmarkar's algorithm for linear programming. AKS primality test quasi-polynomial time QP , Best-known approximation algorithm for the directed Steiner tree problem, best known parity game solver, best known graph isomorphism algorithm sub-exponential time(first definition) SUBEXP for all Contains BPP unless EXPTIME (see below) equals MA. sub-exponential time(second definition) Best classical algorithm for integer factorization formerly-best algorithm for graph isomorphism exponential time(with linear exponent) E , Solving the traveling salesman problem using dynamic programming factorial time Solving the traveling salesman problem via brute-force search exponential time EXPTIME , Solving matrix chain multiplication via brute-force search. Finding a winning strategy in chess, checkers, or Go with Japanese ko rule on an board. double exponential time 2-EXPTIME Deciding the truth of a given statement in Presburger arithmetic
An algorithm is termed constant time (often represented as time) when its complexity function 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 . 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 and to ensure 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 such that the time taken will never exceed , 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 . Since and are related by a constant multiplier, and such a multiplier is irrelevant to big O classification, the standard usage for logarithmic-time algorithms is regardless of the base of the logarithm appearing in the expression of .
Algorithms taking logarithmic time are commonly found in operations on binary trees or when using binary search.
An 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 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 is of the order of .
An example of logarithmic time is given by dictionary search. Consider a dictionary which contains entries, sorted in alphabetical order. We suppose that, for , one may access the -th entry of the dictionary in a constant time. Let denote this -th entry. Under these hypotheses, the test to see if a word is in the dictionary may be done in logarithmic time: consider , where denotes the floor function. If --that is to say, the word is exactly in the middle of the dictionary--then we are done. Else, if --i.e., if the word 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 is for some constant . Another way to write this is .
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 time per insert/delete operation.
Sub-linear time An algorithm is said to run in sub-linear time (often spelled sublinear time) if . 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:
Parallel algorithms that have linear or greater total work (allowing them to read the entire input), but sub-linear depth. Algorithms that have guaranteed assumptions on the input structure. An important example are operations on data structures, e.g. binary search in a sorted array. Algorithms that search for local structure in the input, for example finding a local minimum in a 1-D array (can be solved in time using a variant of binary search). A closely related notion is that of Local Computation Algorithms (LCA) where the algorithm receives a large input and queries to local information about some valid large output.
Linear time An algorithm is said to take linear time, or time, if its time complexity is . Informally, this means that the running time increases at most linearly with the size of the input. More precisely, this means that there is a constant such that the running time is at most for every input of size . For example, a procedure that adds up all elements of a list requires time proportional to the length of the list, if the adding time is constant, or, at least, bounded by a constant.
Linear time is the best possible time complexity in situations where the algorithm has to sequentially read its entire input. Therefore, much research has been invested into discovering algorithms exhibiting linear time or, at least, nearly linear time. This research includes both software and hardware methods. There are several hardware technologies which exploit parallelism to provide this. An example is content-addressable memory. This concept of linear time is used in string matching algorithms such as the Boyer–Moore string-search algorithm and Ukkonen's algorithm.
Quasilinear time An algorithm is said to run in quasilinear time (also referred to as log-linear time) if for some positive constant ; linearithmic time is the case . Using soft O notation these algorithms are . Quasilinear time algorithms are also for every constant and thus run faster than any polynomial time algorithm whose time bound includes a term for any .
Algorithms which run in quasilinear time include: In-place merge sort, Quicksort, , in its randomized version, has a running time that is in expectation on the worst-case input. Its non-randomized version has an running time only when considering average case complexity. Heapsort, , merge sort, introsort, binary tree sort, smoothsort, patience sorting, etc. in the worst case Fast Fourier transforms, Monge array calculation, Schönhage–Strassen algorithm for multiplication,
In many cases, the running time is simply the result of performing a operation times (for the notation, see ). For example, binary tree sort creates a binary tree by inserting each element of the -sized array one by one. Since the insert operation on a self-balancing binary search tree takes time, the entire algorithm takes time.
Comparison sorts require at least comparisons in the worst case because , by Stirling's approximation. They also frequently arise from the recurrence relation .
Sub-quadratic time An algorithm is said to be subquadratic time if .
For example, simple, comparison-based sorting algorithms are quadratic (e.g. insertion sort), but more advanced algorithms can be found that are subquadratic (e.g. shell sort). No general-purpose sorts run in linear time, but the change from quadratic to sub-quadratic is of great practical importance.
Polynomial time
An algorithm is said to be of polynomial time if its running time is upper bounded by a polynomial expression in the size of the input for the algorithm, that is, for some positive constant . Problems for which a deterministic polynomial-time algorithm exists belong to the complexity class P, which is central in the field of computational complexity theory. Cobham's thesis states that polynomial time is a synonym for "tractable", "feasible", "efficient", or "fast".