Darshan Redkar - 11 months ago 78

Java Question

I am a Java Developer, I wanted to know about Complexity of Program and its calculation ? (i am beginner please answer in simple terms that i can understand

thanks in advance..!!)

Answer Source

Generally complexity is a number of operation you have to perform to achieve your goal.

Complexity is marked as O(n) where `n`

is the complexity. For example complexity of assignment is 1.

Complexity of access to array element is 1 too. Complexity of iteration over all elements of array, collection, map etc is n where n is number of elements of the collection. For example if you want to find sum of all elements of n-elements array you have to perform operation with complexity O(n).

Please note that complexity of looking for specific element of array is n also although average number of operations is n/2 because the element may be at first, last or any other position.

Complexity of sorting depends on the algorithm. Simple algorithms sort arrays with complexity of n^2, while better algorithms like quick short have n*ln(n).