Data structure

Binary Search

Binary Search

Binary search comes in the interval search. It is also known as half-interval search, logarithmic search, binary chop. For applying binary search, the data structure should be sorted. In binary search sorted array is divided into the two parts repeatedly and searching occurs in that part of the search is key is less than the item in the middle in the interval. This process has occurred until the searched element is found or the interval is empty.

In binary search, half of the elements are ignored just after one comparison which makes it a good searching algorithm.

Time complexity: Worst case-O(logn)

                                 Best case- O(1)

              Algorithm: An array A of n elements with values  

                   Step 1. Set L to 0 and R to n-1.

                   Step 2. Apply condition, If L>R  the search terminates as unsuccessful.

                   Step 3.Set m (the middle element) by calculating (L+R)/2  

                   Step 4.If m>k, set R to m-1 and go to step 2.

                   Step 5. If m<k, set L to m+1 and go to step 2

                   Step 6. Now if m=k the search is done; other no such element found.