Time complexity of binary search for an unsorted array

前端 未结 3 760
青春惊慌失措
青春惊慌失措 2020-12-25 09:19

I am stuck up with two time complexities. To do a binary search with sorted array is O(logN). So to search an unsorted array we have to sort it first so that becomes O(NlogN

3条回答
  •  无人及你
    2020-12-25 09:29

    The time complexity of linear search is O(n) and that of binary search is O(log n) (log base-2). If we have an unsorted array and want to use binary search for this, we have to sort the array first. And here we have to spend a time O(n logn) to sort the array and then spend time to search element.

提交回复
热议问题