Comparing algorithms
We will consider two ways of comparing algorithms: time complexity and space complexity. Time complexity is the time it will take for an algorithm to execute. Space complexity is the amount of memory required during the
execution of the algorithm.
Constant time
An algorithm with constant time will always take the same amount of time to execute.
Linear time
Linear functions are generally expressed in the form:
f(x) = ax + b
As the value of x increases, the relative value of the constant, b, added to the first term is less and less relevant.
f(x) = 2x + 3
f(10) = 2 x 10 + 3 = 23
f(100,000) = 2 x 100,000 + 3 = 200,003
Polynomial time
Polynomial functions are generally expressed in the form:
f(x) = ax2 + bx + c
As the value of x increases, the relative value of the bx and the constant, c, added to the first term are less and less relevant.
f(x) = 2x2 + 3x + 8
f(10) = 2 x 102 + 3 x 10 + 8 = 238
f(100,000) = 2 x 100,0002 + 3 x 100,000 + 8 = 20,000,300,008
Exponential time
Exponential functions are expressed in the form:
f(x) = abx
Exponential functions become very large very quickly as the size of x grows.
f(x) = 2x
f(10) = 210 = 1024
f(100,000) = 2100,000 = approaching infinity (30,103 digits).
Another type of exponential function is of the form:
f(x) = x!.
x! means x factorial.
f(5) = 5! = 5 x 4 x 3 x 2 x 1 = 120
Logarithmic time
Logarithmic functions take the form of:
f(x) = a lognx
The log of a number is the power you would have to raise the base to in order to reach it.
f(x) = log2x
f(10) = log210 = 3.322
f(100,000) = log2100,000 = 16.61