0

this may seem like a basic question about algorithms, but trying to confirm to myself that I'm correct (still trying to grasp basic concepts in teaching myself algortihms).

  • a problem i have requires first running an O(N) algorithm
  • then performing N binary searches on an N-element array
  • then running another O(N) algorithm

I'm trying to find the total time complexity for this problem.

Would it just be O(N) because that's the dominant order over O(logN)?

Thanks in advance

0

1 Answer 1

4

An O(N) step would indeed dominate an O(log N) step. But your second step doesn't take O(log N) time. Each binary search takes O(log N) time and you perform binary search N times. So the second step, and thus the whole algorithm, takes O(N log N) time.

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

2 Comments

i originally actually thought you sum up the time complexities. so it would be O(n) + O (log n) + O(n) which is O(2n) + O(logn) with O(2n) as the dominant order. why is that thinking wrong? thanks!
@WycG That rule is correct, your second term is wrong: It's not O(log n) but O(n log n) for reasons outlined above. n * O(log n) = O(log n) + O(log n) + ... + O(log n) = O(n log n) for the same reason O(n) + O(n) = O(2n), but a factor of 2 is ignored while a factor of n isn't.

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.