Posts

Showing posts from 2018

Binary Search

Binary Search Try out this game before Beginning, https://www.khanacademy.org/computing/computer-science/algorithms/intro-to-algorithms/a/a-guessing-game Well you must have thought of some strategy to find the number in optimal way. Iterating through all the numbers is not possible with the given constraints. Let’s look at one of the Strategy( I guess only one thats intuitive ;P) At its most fundamental level, a Binary search is employed to find a value in a sorted sequence. Eg:- An array of real integers. Binary_Search(A, value): beg=1 , end = A.size While beg <= end: Mid = floor((beg+end)/2) If A[mid] == value : Return mid; // Return the position of the value Else if A[mid] < value : beg=mid+1;  // Search the upper half of the array Else end=mid-1;  // Search the lower half of the array     Return -1;      // Indicates target not found If you analyze the pseudo code properly, you will realize t