JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

Showing posts with label Binary-Search. Show all posts
Showing posts with label Binary-Search. Show all posts

what is Binary Search?

11:08 PM
Binary Search Binary search is the search technique which works efficiently on the sorted lists. Hence, in order to search an element into some list by using binary search technique, we must ensure that the list is sorted. Binary search follows divide and conquer approach in which, the list is divided into two halves and the item is compared with the middle element of the list. If the match is found then, the location of middle element is returned otherwise, we search into either of the halves depending upon the result produced through the match. Binary search algorithm is given below. BINARY_SEARCH(A, lower_bound, upper_bound, VAL) Step 1:  [INITIALIZE] SET BEG = lower_bound END = upper_bound, POS = - 1 Step 2:  Repeat Steps 3 and 4 while BEG <=END Step 3:  SET MID = (BEG + END)/2 Step 4:  IF A[MID] = VAL SET POS = MID PRINT POS Go to Step 6 ELSE IF A[MID] > VAL SET END = MID - 1 ELSE SET BEG = MID + 1 [END OF IF] [END OF LOOP] Step 5:  IF POS = -1 PRINT "VA

.