Skip over navigation

Binary Search

Problems

What is Binary Search

Binary Search as Applied to Arrays

Problem : What are the best, worst, and average case times of binary search?

Where n is the number of data items being search, the best, worst, and average case times are all O(logn) .

Problem : If there are 22,049 data elements being searched, what is the maximum number of "looks" it will take with binary search to find the data element being search for?

At most it will take 15 "looks". The log(22, 049 is approximately 14.4.

Problem : Will binary search always be faster than linear search, even on a large data set?

No. For example, if the item being searched for is the first item in the list, the linear search will find it on its first look, while binary search will take the maximum number of looks, logn .

Problem : Why won't binary search work on linked lists?

Binary search requires a data structure that supports random access. In other words, binary search requires the ability to look immediately at any item in the data set, given an index number for it. With linked lists, one would have to traverse O(n) items to find a single item in the list, thereby nullifying the positive efficiency contributions of binary search.

Problem : Sorting of a data set can be done in O(nlogn) time. You have in front of you a large data set that is in unsorted order. You need to complete n searches on this data set. Does it make more sense to use linear search, or to sort it and use binary search.

It makes more sense to sort it and use binary search. To do n linear searches will take n*O(n) = = O(n 2) time. To sort it and do n binary searches will take O(nlogn) + n*O(logn) = = O(nlogn) time.

Follow Us