B
B
bondle2021-12-27 21:16:33
Algorithms
bondle, 2021-12-27 21:16:33

What will be the complexity of this algorithm?

The code inside the loop calls the sort function with O(n log n) complexity:

for item in N:
   sort(item)
   ...

On the one hand, it seems that the complexity is N, but each iteration we call n log n, so this is O(n^2 log n)? What will be its difficulty?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2021-12-27
@wataru

Depends on the size of the item. If they are there each O(N) size, then your guess is correct: O(n^2 log n).
If all items do not exceed N in total, then the answer is O(N log N). Because if their sizes are a_i, then you have the sum ai log ai. log(ai) can be limited from above to log(N) and put out of brackets and then replace the sum in brackets with N.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question