Is O(n) considered as faster compared to O(n log n)? If I have a function that does a loop, which is O(n) then a merge sort outside the loop O(n log n) then the run time would be O(n log n) I assume?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, not directly. Big-O notation is about the limiting factor in an algorithm, which has more to do with an algorithms scalability than speed. You can have a routine that’s O(1) which takes longer than a routine of O(n^2) for a specific set of data – but the former will scale much better.
That being said, in general, O(n) will of course scale better than O(n log n), and would likely be considered "faster" or "better".
It’s unclear exactly what you’re saying here –
If you mean you have a loop with 2 functions, ie:
Then the overall limiting factor is going to be O(n^2 log n).
If, instead, you’re saying you’re going to do something like:
Then the overall complexity is still O(n log n), as that is the limiting factor. This is because "O(n + n)" is still O(n).