Binary search is an efficient algorithm for searching sorted arrays that works by comparing the search key to the middle element of the array and recursively searching either the upper or lower half. The best case runs in O(1) time if the key is in the middle, while the worst case runs in O(log n) time if the key is at the end of the array or not present. Binary search is faster than linear search for large sorted arrays but requires the array to be sorted first and only works for data structures that support direct access.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
33 views
Algorithms-Analysis Framework Binary Search
Binary search is an efficient algorithm for searching sorted arrays that works by comparing the search key to the middle element of the array and recursively searching either the upper or lower half. The best case runs in O(1) time if the key is in the middle, while the worst case runs in O(log n) time if the key is at the end of the array or not present. Binary search is faster than linear search for large sorted arrays but requires the array to be sorted first and only works for data structures that support direct access.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15
•1
Algorithms- Analysis Framework
Binary search Binary search •2
Binary search is a remarkably efficient algorithm for
searching in a sorted array. It works by comparing a search key K with the array’s middle element A[m]. If they match, the algorithm stops; otherwise, the same operation is repeated recursively for the first half of the array if K <A[m], and for the second half if K >A[m] Binary search algorithm •3 Binary search complexity •4
How many such comparisons does the algorithm
make on an array of n elements? The answer obviously depends not only on n but also on the specifics of a particular instance of the problem. Binary search complexity-Best •5 case How many such comparisons does the algorithm make on an array of n elements? Best Case: In the best case, where the target value is the middle element of the array, its position is returned after one iteration. It means, regardless of the size of the list/array, we’ll always get the result in constant time. So the best case complexity is O(1). Binary search complexity-worst •6 case Worst case: In worst case scenario, the binary search finds the item at the end of the list/array when it’s narrowed down to a single item. The worst case may also be reached when the target element is not in the array. If n is one less than a power of two, then this is always the case. Binary search complexity-worst •7 case 32/2 --> 16 // 1 16/2 --> 8 // 2 8/2 --> 4 // 3 4/2 --> 2 // 4 2/2 --> 1 // 5 Total = 5 guesses/comparisons. If we double the size to 64 then only one more comparison is extra made. 64/2 --> 32 // 1 32/2 --> 16 // 2 16/2 --> 8 // 3 8/2 --> 4 // 4 4/2 --> 2 // 5 2/2 --> 1 // 6 Total = 6 guesses/comparisons. Binary search complexity-worst •8 case If we look at the comparison and size of the input we see a pattern here eg. 32 = 2⁵ and 64 = 2⁶. Let’s assume n is the size of input and k is the number of comparisons then we can say that
Let’s apply logarithm to both sides with base 2
By the help of the equation above we can make a statement
that the worst case complexity of binary search is O(log n). Solving using recurrence relation O(log (n+1)). Binary search complexity-worst •9 case The logarithmic function grows so slowly that its values remain small even for very large values of n. It will take no more than log2(103 + 1) = 10 three- way comparisons to find an element of a given value (or establish that there is no such element) in any sorted array of one thousand elements. It will take no more than log2(106 + 1) = 20 comparisons to do it for any sorted array of size one million! Limitations of Binary Search •10
Binary search can be applied iff items are sorted
otherwise one must sort items before applying it. And we know that sorting is relatively an expensive operation. Binary search can only be applied to data structures which allow direct access to elements. If we do not have direct access then we can not apply binary search on it eg. we can not apply binary search to Linked List. Linear Search Usage •11
Linear search can be done on a linked list, which
allows for faster insertion and deletion than an array. Binary Search Usage •12
Sorted arrays with binary search are a very
inefficient solution when insertion and deletion operations are interleaved with retrieval, taking O(n) time for each such operation. Binary search can be used to perform exact matching and set membership (determining whether a target value is in a collection of values) Binary Search on a sored data Binary vs Sequential Search Summary •15
Binary search is faster than linear search for sorted
arrays except if the array is short, although the array needs to be sorted beforehand. There are operations such as finding the smallest and largest element that can be done efficiently on a sorted array but not on an unsorted array