2

In java, if I sort using Arrays.sort() (n log n) and then use a for loop o(n) in the code, what will be the new complexity ? is it n^2 log n or n log n

1

2 Answers 2

4

Answer: O(nLog(n))

non nested complexities can be simply added. i.e. O(n) + O(nLog(n))

For large n, nLog(n) is significantly greater than n. Therefore, O(nLog(n)) is the answer.

Read this: https://en.wikipedia.org/wiki/Big_O_notation

Note:

if the complexities are nested then the complexities are multiplied, for example:

Inside a loop of order n, you are doing a sort of order nLog(n).

Then complexity will be O(n * nLog(n)). i.e. O(n²Log(n))

Sign up to request clarification or add additional context in comments.

Comments

4

If you perform the for loop after, you have an O(nlog(n)) operation followed by an O(n) one. Since O(n) is negligible compared to O(nlog(n)), your overall complexity would be O(nlog(n)).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.