What is Complexity Of A Code?

what do you mean by complexity of a algorithm in layman’s term. How can one find it?

total expense per operation, evaluated over a sequence of operations.

This is known as the Big-O notation.

Briefly:

  1. O(1) means in constant time - independent of the number of items,

  2. O(N) means in proportion to the number of items,

  3. O(log N) means a time proportional to log(N).

Basically any ‘O’ notation means an operation will take time up to a maximum of k*f(N)
where:

k is a constant multiplier and f() is a function that depends on N.

Quick Explanation:

  1. O(N) means that your algorithm will take on the order of n operations to insert an item. e.g. looping through the list once (or a constant number of times such as twice or only looping through half).

  2. O(N2) means that for every insert, it takes N2 operations. i.e. 1 operation for 1 item, 4 operations for 2 items, 9 operations for 3 items and so on.

Note : Here N refers to the size of input.

Other Links : Read here for further explanation.

1 Like

and how is it calculated? what does O(n) & nO(n) means?