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(nlogn), 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(logn). 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(logn) 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(logn) 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((logn)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.

