Complexity

Complexity

To develop efficient programs, it is important to analyse the complexity of our programs and its code.

Complexity is a measure of how well a program performs.

The main measures of complexity, is time complexity and space complexity

We express complexity in Big-O notation.

Constant (BEST) - $O(1)$
Logarithmic - $O(log\ n)$
Linear - $O(n)$
n-log-n - $O(n\ log\ n)$
Quadratic - $O(n^2)$
Cubic - $O(n^3)$
Exponential - $O(2^n)$
Factorial - $O(n!)$
Tetratial (WORST) - $O(n^n)$

Time Complexity

Time Complexity

Time complexity is the measure of how long an algorithm will take to complete. Often we assess time complexity on searching and sorting algorithms.

With small data sets, any searching/sorting algorithm will be virtually instant, but with large data sets it can take a long time.

There is no ‘best’ searching/sorting algorithm, as it will depend on the nature of the data.

To calculate the time complexity of an algorithm, we find the number of operations performed in its worst case scenario.

Example - Linear Search
Best case (first item matched) - 1 access - $O(1)$
Worst case (last item matched) - n accesses - $O(n)$

The worst case is $O(n)$ accesses.
We look at the highest power of n, without any coefficients. In our case, it is just $O(n)$

Space Complexity

Space Complexity

Space complexity is a measure of how much space a data structure, or an algorithm requires.
Generally, time complexity contributes more towards a program’s efficiency than its space complexity.

For most data structures, n items will naturally have a space complexity of $O(n)$.

More

You can find a list of the complexities of many data structures and algorithms here

www.bigocheatsheet.com

Home