WycG
WycG

Reputation: 141

total time complexity of basic algorithm with binary search

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).

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

Upvotes: 0

Views: 130

Answers (1)

user395760
user395760

Reputation:

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.

Upvotes: 4

Related Questions