What is the difference between Linear search and Binary search?
A linear search looks down a list, one item at a time, without jumping. In complexity terms this is an O(n)
search - the time taken to search the list gets bigger at the same rate as the list does.
A binary search is when you start with the middle of a sorted list, and see whether that's greater than or less than the value you're looking for, which determines whether the value is in the first or second half of the list. Jump to the half way through the sublist, and compare again etc. This is pretty much how humans typically look up a word in a dictionary (although we use better heuristics, obviously - if you're looking for "cat" you don't start off at "M"). In complexity terms this is an O(log n)
search - the number of search operations grows more slowly than the list does, because you're halving the "search space" with each operation.
As an example, suppose you were looking for U in an A-Z list of letters (index 0-25; we're looking for the value at index 20).
A linear search would ask:
list[0] == 'U'
? No.list[1] == 'U'
? No.list[2] == 'U'
? No.list[3] == 'U'
? No.list[4] == 'U'
? No.list[5] == 'U'
? No.
...list[20] == 'U'
? Yes. Finished.
The binary search would ask:
Compare
list[12]
('M') with 'U': Smaller, look further on. (Range=13-25)
Comparelist[19]
('T') with 'U': Smaller, look further on. (Range=20-25)
Comparelist[22]
('W') with 'U': Bigger, look earlier. (Range=20-21)
Comparelist[20]
('U') with 'U': Found it! Finished.
Comparing the two:
- Binary search requires the input data to be sorted; linear search doesn't
- Binary search requires an ordering comparison; linear search only requires equality comparisons
- Binary search has complexity O(log n); linear search has complexity O(n) as discussed earlier
- Binary search requires random access to the data; linear search only requires sequential access (this can be very important - it means a linear search can stream data of arbitrary size)
Think of it as two different ways of finding your way in a phonebook. A linear search is starting at the beginning, reading every name until you find what you're looking for. A binary search, on the other hand, is when you open the book (usually in the middle), look at the name on top of the page, and decide if the name you're looking for is bigger or smaller than the one you're looking for. If the name you're looking for is bigger, then you continue searching the upper part of the book in this very fashion.
A linear search works by looking at each element in a list of data until it either finds the target or reaches the end. This results in O(n) performance on a given list. A binary search comes with the prerequisite that the data must be sorted. We can leverage this information to decrease the number of items we need to look at to find our target. We know that if we look at a random item in the data (let's say the middle item) and that item is greater than our target, then all items to the right of that item will also be greater than our target. This means that we only need to look at the left part of the data. Basically, each time we search for the target and miss, we can eliminate half of the remaining items. This gives us a nice O(log n) time complexity.
Just remember that sorting data, even with the most efficient algorithm, will always be slower than a linear search (the fastest sorting algorithms are O(n * log n)). So you should never sort data just to perform a single binary search later on. But if you will be performing many searches (say at least O(log n) searches), it may be worthwhile to sort the data so that you can perform binary searches. You might also consider other data structures such as a hash table in such situations.