- Related Topics:
- computational complexity
time complexity
-
What is time complexity?
In computer science, time complexity describes the amount of computer time required to run an algorithm. It is one of the two most discussed types of computational complexity, the other being space complexity.
Time complexity is typically written as T(n), where n relates to the input size. Big-O notation describes T(n) to refer to the order, or kind, of growth that the function experiences as the number of elements in the function increases.
For example, an algorithm with constant time, or O(1), always uses the same number of operations, regardless of the number of elements being operated on. Linear time, or O(n), indicates that the time it takes to run an algorithm grows linearly as n increases.
2 Britannica SourcesCreated from Britannica articles using AI. AI can make mistakes, so verify using Britannica articles. -
Why is time complexity important in mathematics and computer science?
Time complexity is a description of the amount of computer time required to run an algorithm. Understanding the time complexity of an algorithm allows programmers to select the algorithm best suited for their needs; a fast algorithm that is good enough is often preferable to a slow algorithm that performs better along other metrics.
Computer scientists use mathematical measures of complexity that allow them to predict, before writing the code, how fast an algorithm will run and how much memory it will require. Such predictions are important guides for programmers implementing and selecting algorithms for real-world applications.
4 Britannica SourcesCreated from Britannica articles using AI. AI can make mistakes, so verify using Britannica articles. -
What do we mean by 'input size' when talking about time complexity?
In time complexity, input size refers to the amount of data an algorithm processes. Time complexity, denoted as T(n), assesses how the operations needed scale with input size n. Big-O notation describes this growth, indicating the order of complexity as n increases.
For example, O(n) (linear time) means the time grows proportionally with n, while O(n^2) (quadratic time) means it grows with the square of n, and O(log n)log n (logarithmic time) means that the time grows as a logarithm of n. Problems solvable in polynomial time are efficient, whereas those requiring exponential time are impractical.
1 Britannica SourceCreated from Britannica articles using AI. AI can make mistakes, so verify using Britannica articles. -
What is Big O notation and how does it relate to time complexity?
Big O notation describes how an algorithm's runtime scales with input size. It is used to represent the time complexity of an algorithm, with time complexity referring to the amount of computer time required to run an algorithm.
- O(1) (constant time) means the algorithm takes the same time regardless of input size.
- O(n) (linear time) means the runtime grows linearly with the input.
- O(log n) (logarithmic time) signifies the runtime grows logarithmically with the input, like in a binary search.
- O(n^2) (quadratic time) indicates the runtime grows proportionally to the square of the input size, as seen in selection sort.
2 Britannica SourcesCreated from Britannica articles using AI. AI can make mistakes, so verify using Britannica articles. -
Can you give examples of different types of time complexity, like O(1), O(n), or O(n^2)?
Time complexity describes the computer time needed to run an algorithm and is written as T(n), where n relates to the input size. Big-O notation describes the growth of the function as the number of elements increases.
Examples of time complexities include:
* Constant time, or O(1), means the algorithm uses the same number of operations regardless of the number of elements.
* Linear time, or O(n), indicates the time to run an algorithm grows linearly as n increases.
* Logarithmic time, or O(log n), means the time needed grows as a logarithm of n.
* Quadratic time, or O(n^2), indicates the time to run an algorithm grows as the square of n.2 Britannica SourcesCreated from Britannica articles using AI. AI can make mistakes, so verify using Britannica articles. -
How can understanding time complexity help us choose better algorithms for solving problems?
Understanding time complexity helps programmers choose the best algorithm for their needs. Time complexity describes how much computer time is required to run an algorithm.
Estimates of time complexity use mathematical models to estimate the number of operations a computer will need to execute an algorithm. Time complexity is typically written as T(n), where n is a variable related to the size of the input. Big-O notation is used to refer to the order, or kind, of growth the function experiences as the number of elements in the function increases.
For example, a fast algorithm that is good enough is often preferable to a slow algorithm that performs better along other metrics. Sorting algorithms are a vital building block of many applications, including search tools, data analysis, and e-commerce.
2 Britannica SourcesCreated from Britannica articles using AI. AI can make mistakes, so verify using Britannica articles.
time complexity, a description of how much computer time is required to run an algorithm. In computer science, time complexity is one of two commonly discussed kinds of computational complexity, the other being space complexity (the amount of memory used to run an algorithm). Understanding the time complexity of an algorithm allows programmers to select the algorithm best suited for their needs, as a fast algorithm that is good enough is often preferable to a slow algorithm that performs better along other metrics.
Estimates of time complexity use mathematical models to estimate how many operations a computer will need to run to execute an algorithm. Because the actual time it takes an algorithm to run may vary depending on the specifics of the application of the algorithm (e.g., whether 100 or 1 million records are being searched), computer scientists define time complexity in reference to the size of the input into the algorithm. Time complexity is typically written as T(n), where n is a variable related to the size of the input. To describe T(n), big-O notation is used to refer to the order, or kind, of growth the function experiences as the number of elements in the function increases.
Constant time, or O(1), is the time complexity of an algorithm that always uses the same number of operations, regardless of the number of elements being operated on. For example, an algorithm to return the length of a list may use a single operation to return the index number of the final element in the list. O(1) does not imply that only one operation is used but rather that the number of operations is always constant.
Linear time, or O(n), indicates that the time it takes to run an algorithm grows in a linear fashion as n increases. In linear time, searching a list of 1,000 records should take roughly 10 times as long as searching a list of 100 records, which in turn should take roughly 10 times as long as searching a list of 10 records.
Logarithmic time, or O(log n), indicates that the time needed to run an algorithm grows as a logarithm of n. For example, when a binary search on a sorted list is performed, the list is searched by dividing it in half repeatedly until the desired element is found. The number of divisions necessary to find the element grows with the logarithm of n in base 2 rather than proportionally to n. O(log n) is a slower growth rate than O(n); thus, these algorithms have lower time complexity than linear time algorithms.
Quadratic time, or O(n2), indicates that the time it takes to run an algorithm grows as the square of n. For example, in a selection sort algorithm, a list is sorted by repeatedly finding the minimum value in the unsorted part of the list and placing this value at the beginning. Because the number of operations needed to find the minimum value in the list grows as the length n of the list grows, and the number of values that must be sorted also grows with n, the total number of operations grows with n2. These algorithms grow much faster than those that grow in linear time.
Big-O notation can be used to describe many different orders of time complexity with varying degrees of specificity. For example, T(n) might be expressed as O(n log n), O(n7), O(n!), or O(2n). The O value of a particular algorithm may also depend upon the specifics of the problem, and so it is sometimes analyzed for best-case, worst-case, and average scenarios. For example, the Quicksort sorting algorithm has an average time complexity of O(n log n), but in a worst-case scenario it can have O(n2) complexity.
Problems that can be solved in polynomial time (that is, problems where the time complexity can be expressed as a polynomial function of n) are considered efficient, while problems that grow in exponential time (problems where the time required grows exponentially with n) are said to be intractable, meaning they are impractical for computers to solve. For example, an algorithm with time complexity O(2n) quickly becomes useless at even relatively low values of n. Suppose a computer can perform 1018 operations per second, and it runs an algorithm that grows in O(2n) time. If n = 10, the algorithm will run in less than a second. However, if n = 100, the algorithm will take more than 40,000 years to run.

" height="20" alt="HEW Logo">