Nepedia
Back to Discover
Advanced Reader
💻

Time complexity

technology Maturity 11-13 Next article →

In theoretical computer science, time complexity is a way to describe computational complexity. It measures how much computer time an algorithm needs to finish a task. Because exact seconds are hard to measure, scientists count elementary operations instead. An elementary operation is a simple, basic step performed by a computer. We assume each of these steps takes a fixed amount of time. This allows us to relate the total time to the number of operations performed.

comparison computational complexity
comparison computational complexity

An algorithm's running time can change depending on the specific input it receives. For example, a list might be already sorted or completely scrambled. Because of this variation, researchers often focus on worst-case time complexity. This represents the maximum amount of time required for any input of a certain size. Another method is average-case complexity, which calculates the average time for all possible inputs of a given size. Since there are a finite number of inputs for any size, this average is possible to determine.

To describe these speeds, scientists study asymptotic behavior. This means they look at how the time requirement changes as the input size increases. For small inputs, the exact time might not matter very much. However, as the input size grows, the way the time grows becomes very important. We use big O notation to express this relationship. In this notation, we use the letter O followed by a function of n. Here, n represents the size of the input, such as the number of bits used to represent it.

comparison computational complexity
comparison computational complexity

Algorithms are classified into different complexity classes based on their big O notation. Constant time, written as O(1), is the fastest type. In constant time, the time required is bounded by a value that does not depend on the input size. For instance, accessing a single element in an array takes constant time. Similarly, finding the minimum value in a list that is already sorted in ascending order is O(1). Even if the task takes a bit of time, it remains constant if an upper bound exists that stays the same regardless of n.

Logarithmic time, or O(log n), is another highly efficient class. This occurs when the ratio of operations to input size decreases toward zero as n increases. A classic example is a binary search. In a dictionary search, you might look at the middle entry first. If your word comes earlier, you discard the right half and repeat the process on the left half. This repeatedly halves the search space, making the process very fast. Polylogarithmic time, written as O(log^k n), is a slightly different class used for more complex tasks.

Linear time, or O(n), means the running time increases at a steady rate relative to the input. If you have to read every single item in a list once, you are likely performing a linear time operation. This is the best possible speed for any algorithm that must sequentially read its entire input. Examples include linear search and Kadane's algorithm. Quasilinear time, also called linearithmic time, is written as O(n log n). This is slightly slower than linear time but much faster than quadratic time. Common examples include the Fast Fourier transform and several efficient sorting algorithms like merge sort and heapsort.

As algorithms become more complex, they may fall into polynomial or exponential classes. Polynomial time, or O(n^k), means the time is bounded by a polynomial expression. Problems that can be solved in polynomial time are called tractable or feasible. This class is central to computational complexity theory and includes the class P. In contrast, exponential time, such as O(2^n), grows extremely quickly. Solving the traveling salesman problem via brute-force search is an example of an exponential time task. Other even slower classes include factorial time and double exponential time, which are used for very difficult problems like finding winning strategies in certain board games.

comparison computational complexity
comparison computational complexity
656 words

Where will your curiosity go next?

Keep exploring at your chosen reading level.

🖼️ Images & Media (1)
File:comparison computational complexity.svg
comparison computational complexity.svg
More to explore

What is Nepedia?

A free, ad-free encyclopedia for children. Every article is written at five reading levels, so the same page works for a five-year-old and a fifteen-year-old — use the level switcher above to see this one change. No account needed to read.