time complexity of algorithms
let
P
be a computational problem, the complexity of P
is the time complexity of the fastest possible algorithm that solves P
the time complexity of an algorithm is defined as the amount of time taken by it to run, as a function of the length of the input
let
be a function over the natural numbers, to prove the time complexity of
we have to show that:
P
be a computational problem, let P
to be equal to - there exists an algorithm that solves
P
whose time complexity is - the time complexity of every algorithm that solves
P
is atleast, i.e. the complexity is bound from below by
take the 
_proving the upper bound_:
we already know that there exists a solution to
_proving the lower bound_:
we have to show that there exists no
because every
its time complexity cannot be lower than 
merge
algorithm of merge sort as an example, the time complexity of this algorithm is _proving the upper bound_:
we already know that there exists a solution to
merge
whose time complexity is _proving the lower bound_:
we have to show that there exists no
merge
algorithm whose time complexity is less than because every
merge
algorithm would have to construct the output vector whose size is consider the 
_proving the lower bound_:
we already know that there exist
time
_proving the upper bound_:
we have to show that there exists no
because every
sum
problem, i.e. a function that sums the numbers in its input vector, its time complexity is _proving the lower bound_:
we already know that there exist
sum
functions that run in _proving the upper bound_:
we have to show that there exists no
sum
algorithm whose time complexity is less than because every
sum
algorithm would have to check each item in its input vector atleast ones, we know it has to perform atleast n
operations, which means that its time complexity cannot be lower than best case
best case is the time complexity of the case with the least work done, i.e. the case in which the time complexity is minimal
average case
the average-case complexity of an algorithm is the amount of some computational resource (typically time) used by the algorithm, averaged over all possible inputs.
worst case
worst case is the time complexity of the case with the most work done, i.e. the case in which the time complexity is maximal
best conceivable runtime
the best conceivable runtime means it is the best time you can conceive that the problem could possibly be solved in, and it is definitely impossible for the problem to be solved faster than that
examples
we track the variable
lastp
, every time we enter the third inner loop it is decremented and is never incremented until it reaches a point at which the while
loop wouldnt execute anymore, this can happen N
times at mosteach time the first main loop run, the second and third inner loop both may in total run
N
times at most because they are both bound by lastp
and j
which get closer to each other every time either loop runsand so the time complexity of the two inner loops is bound by