0

what will the time complexity be if I merge sort an array and apply a binary search function on it ? will it be O(log n)?

because

o(log n) + o(n log n) = O(log n)
6
  • 5
    Soerting is O(NlogN); you won't be able to reduce the time below that. And O(logN) + O(NlogN) = O(NlogN), not O(logN). Commented Jul 16, 2020 at 15:29
  • Is n*x, where n is an awfully large positive number and x is some positive number, larger than x by itself? Now, is n*x + x closer to n*x or x? Commented Jul 16, 2020 at 15:34
  • @JonathanLeffler do you have any idea or hints at how you can search for an element with a time complexity of O(log n) Commented Jul 16, 2020 at 15:39
  • If you start with sorted data and maintain it as data is added and removed, then you won't have to perform a sort before searching. Commented Jul 16, 2020 at 15:49
  • @ChristianGibbons i have an UNSORTED array and i want to find an element in the array with a time complexity of O(log n) Commented Jul 16, 2020 at 15:53

1 Answer 1

1

Indeed, the time complexity for Merge Sort, in the average case, is O(n log n). The one for Binary Search is O(log n).

Adding both methods, you have a complexity of O(n log n) instead of the one you said. Imagine that you are adding 1(log n) + n(log n), which is n+1(log n) --> (n log n).

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

Comments

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.