Time complexity

  • You can get it counting the number of operations
  • Time complexity is defined as a function using Big O notation.
  • n is the size of the input.
  • O is the worst case scenario.
  • The O function is the growth rate in function of the input size n.

O(1) Constant : odd or even / look up table / check if item in array is null / print first element from a list / find on a map.

O(log n) Logarithmic: find in sorted array with binary search.

O(n) Linear: find max in unsorted array / duplicate elements in array with Hash map / find / print all values.

Algorithms running times:

Factorial complexity(no good),

exponential complexity(no good),

polinomial complexity (no good),

linearithmic complexity (middle point),

linear time complexity (good)(for loop in one level array, searching, printing) O(n),

logarithmic complexity (good) O(log n),

constant time complexity (best one)(swap variables). O(1)