-1

I want to ask, I'm still learning about time complexity. I got a problem, the question asked me to calculate the time complexity of a binary search,

The time complexity of a binary search is O(log m).

After the search, they want me to calculate a linear O(n) search inside of it. So inside a binary search, there is a linear search.

Is it O(n log m) or O(log m*n)?

And if you could add, please tell me how to calculate the complexity if there is multiple algorithm combine. Thank you!

Dio
  • 1
  • 2
  • 2
    What are m and n, how are they defined? And how exactly are you combining the two algorithms? – kaya3 Nov 12 '20 at 23:33

2 Answers2

1

I'm not sure I fully understood you but I will do my best:
There are 2 possible scenarios which I think are relevant to your question:

  1. There is a loop inside a loop, where one does not effect the other. In that case, you always multiply the time complexity of one by the other one's. In your case for example, if you follow a binary search in the external loop and a linear search inside, should be done in O(n*logn). The rule is relevant for 3 loops inside each other as well, etc. Just multiply one by another.

  2. Some of the nested loops affect some others (ie. different iterations of an outer loop causes a change to the time complexity of an inner loop). In this case calculating the overall complexity of the nested loops becomes more complicated and the above method won't work.

Anyway I do not see any way to get O(log m*n) in your case.

Also, I did not really understand why you switched between n and m. Typically if you talk about the same data structure, with the same number of items, then you use n.

TS_
  • 319
  • 1
  • 5
  • Reading again your question- is the second search is after the first one or inside of it? because if it's just one after another than the complexity is the sum of both, which is: o(n) + o(logn) = o(n) – TS_ Nov 13 '20 at 02:02
  • Thank you, it's inside of it btw. My linear only shows for going forward or backward so it's 1. Thank you very much. – Dio Nov 13 '20 at 03:00
  • `[For a] loop inside a loop, when one does not effect the other [...] you always multiply the time complexity of one by the other one's.` This is a really neat summary. I'm going to steal this line! =P – Elliott Aug 15 '21 at 05:46
  • Thank u! pls upvote if you find it useful :) – TS_ Aug 16 '21 at 17:02
0

Time complexity Will be O(n log m).

In order to calculate complexity find the primitive steps then calculate the recurrence relation. you will get the answer.

for more information you can go to https://iq.opengenus.org/binary-search-in-linked-list/

parvat raj
  • 36
  • 3
  • Would you mind sharing how you know what the questioner's algorithm is? – Elliott Aug 15 '21 at 05:42
  • if the algorithm is of binary search, possibility of linear search inside it is that, data structure that may have been used is linked-list which take O(n) complexity to reach to nth element – parvat raj Aug 15 '21 at 09:44
  • Do you mean that you think that it's a contiguous array of pointers (sorted according to what we're searching for), `A`, where each pointer in `A` points to a linked list? If that were the case the complexity of a search would be `O(log(m) + n)` where `m` is number of elements in `A`, and `n` is the *most* number of elements in any of the lists. However, the original question seems to be too vague regarding what algorithm they're interested in to be clear about any of this. – Elliott Aug 15 '21 at 11:02