What is Algorithm Complexity?
Algorithm complexity is a measure of how efficient an algorithm is, typically expressed using Big O notation
.
Big O notation describes how the execution time or memory usage of an algorithm grows as the input size increases and is presented in the following forms:
Notation | Name | Description |
---|---|---|
O(1) | Constant Time Complexity | Takes constant time regardless of the input size |
O(log n) | Logarithmic Time Complexity | Time grows proportionally to the logarithm of the input size |
O(n) | Linear Time Complexity | Time grows proportionally to the input size |
O(n log n) | Linearithmic Time Complexity | Time grows proportionally to n log n of the input size |
O(nΒ²) | Quadratic Time Complexity | Time grows proportionally to the square of the input size |
O(2βΏ) | Exponential Time Complexity | Time grows proportionally to the exponential of the input size |
Here, the meanings of O
and n
are as follows:
-
'O' (Big O)
: Denotes the order of an algorithm and measures time and space complexity.O
stands forOrder of
, representing the upper bound of the algorithm's computational or memory demands as the input size grows. -
'n'
: Typically represents theinput size
of the algorithm.
For example, the meanings of each complexity are as follows:
-
O(1)
indicates the algorithm's execution time is constant, regardless of the input size. -
O(n)
indicates the algorithm's execution time grows linearly with the input size. -
O(nΒ²)
indicates the execution time grows quadratically with the input size.
Evaluating Algorithm Complexity
Algorithm complexity is evaluated from two perspectives: time complexity
and space complexity
.
Time Complexity
Time complexity represents the time required by an algorithm to solve a problem.
It is usually measured by the number of computational steps needed depending on the input size.
For example, a simple loop runs proportionally to the input size, having linear time complexity, or O(n)
.
In contrast, a nested loop, where another loop occurs inside a for-loop, requires computations proportional to the square of the input size, resulting in quadratic time complexity, or O(nΒ²)
.
Space Complexity
Space complexity represents the amount of memory space required to execute an algorithm.
The amount of memory space includes the size of variables, data structures, and recursive call stacks used by the algorithm.
For example, using only a fixed number of variables results in constant space complexity, or O(1)
.
In contrast, if memory proportional to the input size is needed, it results in linear space complexity, or O(n)
.
Which of the following represents linear time complexity
in Big O notation?
O(1)
O(log n)
O(n)
O(nΒ²)
Guidelines
AI Tutor
Publish
Design
Upload
Notes
Favorites
Help
Code Editor
Execution Result