问题
I understand that the Binary search is more efficient than the Linear search in a sorted list and a large list, but what if we have a large list but not sorted, which one we use linear search or binary search?
回答1:
The concept of binary search can only work on sorted inputs. Just research how it works: Binary search at Wikipedia.
Based on your original question "binary search or linear search on unsorted lists?" the answer clearly is linear search because binary search can not be used.
However could it be possible that you at least have some knowledge of the input structure? If yes you could use that to create a better solution. If it is completely random then obviously linear search is the best. But you could easily parallelize the search like seen here: Fastest way to search for an element in unsorted array.
Let me give you a small overview for binary search. I select a random number between 1 and 100. You can now guess the number and I will tell you if my number is lower, equals or greater than your guess.
Binary search would now guess the half of the search interval, 50. I answer the guess is too high. The search interval is now from 1 to 49 and binary search now goes for 25. It repeats the search until the element is found.
If your input is unsorted then this will not work anymore because if I tell you that my element is lower than 50 then it does not necessarily mean that it is stored left to 50, it could also be to the right because the input is unsorted.
Here is an image illustrating the algorithm (found by a quick Google search):
回答2:
Binary Search by definition only applies to sorted sequences. It is a prerequisite that the list you binary search on has to be sorted, otherwise binary search does not work at all.
So to answer you question: On unsorted sequence linear search is the way to go
But keep in mind that, if you have to perform a number (say M) of searches then it might be good to sort the list once and then use binary search on it.
Imagine you have to perform M
searches on an array of length N
. -
- Using linear search would cost
N*M
(M searches each costing N). - Sorting + binary search would cost
O((N log N)+(log N)*M)
->(N Log N)
for sorting the list, plusM
searches each costingLog N
When is it convenient the former and when the latter? when N*M >N log N+(log N)*M
.
回答3:
In that case Linear Search will be more efficient, because its complexity is O(n)
.
But, for binary search the array needs to be sorted. For sorting the best complexity will be O(nlogn)
, and the search in O(logn)
, which when added gives O(nlogn)
.
So, clearly Linear search is better here.
来源:https://stackoverflow.com/questions/45267908/binary-search-or-linear-search-on-large-unsorted-lists