Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time complexity of binary search for an unsorted array

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). So then we can perform binary search which gives the complexity as O(N) but I have read that it could be O(NlogN). Which is correct?

like image 957
user1521306 Avatar asked Apr 09 '13 20:04

user1521306


1 Answers

Binary Search is for "Sorted" lists. The complexity is O(logn).

Binary Search does not work for "un-Sorted" lists. For these lists just do a straight search starting from the first element; this gives a complexity of O(n). If you were to sort the array with MergeSort or any other O(nlogn) algorithm then the complexity would be O(nlogn).

O(logn) < O(n) < O(nlogn)

like image 67
LuiNova Avatar answered Sep 19 '22 10:09

LuiNova