This notation is called the Big O notation and is used as a shorthand expression for expressing algorithmic complexity (basically, how long this algorithm will take to run when the input size (n) increases)
Generally speaking, you will come across the following basic types of algorithms:
- O (1) - Constant. The duration of this algorithm does not depend on the number of elements that the algorithm should process.
- O (log n) - Logarithmic - The duration this algorithm takes to complete depends on the number of elements that the algorithm should process. As the input size becomes larger, less time is required for each new input.
- O (n) - linear - the time this algorithm takes to complete depends on the number of elements that the algorithm should process. As the input size increases, the time spent on it grows in equal amounts.
- O (n ^ 2) - Polynominal. As the input size increases, the time spent on processing the input increases by a larger and larger number - this means that the large input sizes become unacceptably difficult to solve.
- O (2 ^ n) - Exponential - the most complex types of problems. Processing time increases depending on the size of the input to an extreme degree.
Typically, you can get a rough estimate of the complexity of an algorithm by looking at how it is used. For example, considering the following method:
function sum(int[] x) { int sum = 0; for (int i = 0; i < x.length; i++) { sum += x[i]; } return sum; }
Here are a few things to do here:
- Initialize a variable named sum
- Initialize variable i
- For each iteration i: add x [i] to sum, add 1 to i, check if I am less than x.length
- Refundable amount
There are several operations that are performed for a constant time (the first two and the last), since the size x does not affect the duration. In addition, there are some operations performed in linear time (since they are performed once for each entry in x). With the Big O note, the algorithm is simplified to the most complex, so the algorithm of this sum will work in O (n)
Ryan brunner
source share