Binary Search In Data Structure

  • Binary search is speedy with compare to sequential search.
  • Binary search is apply to the order list.
  • In binary search, we first compare the key with the item in the middle position of the array.
  • If there’s a match, we can return immediately. If the key is less than the middle key, then the item sought must lie in the lower half of the array; if it’s greater then the item sought must lie in the upper half of the array.
  • So we repeat the procedure on the lower (or upper) half of the array

Algorithm

Algorithm is quite simple. It can be done either recursively or iteratively:

  1. get the middle element;
  2. if the middle element equals to the searched value, the algorithm stops;
  3. otherwise, two cases are possible:
    • searched value is less, than the middle element. In this case, go to the step 1 for the part of the array, before middle element.
    • searched value is greater, than the middle element. In this case, go to the step 1 for the part of the array, after middle element.

Binary Search

Example:-

1.)   Data

11
5
10
35
95
89

2.)Apply Sorting  process

0 5
1 10
2 11
3 35
4 89
5 95





1.)  Enter key element (Search Element )By User

2.)  Find the middle Index

Middle index = Int (Stating index + Ending Index) /2

=Int (0+5) /2

Middle index = 2

Middle Element is 11.

3.)  Compare search element with the middle element.

  • If they are same then search is sucessfuk
  • If Search element > middle Element then bottom part of the list maintain
  • If search element is > middle element then upper part of list maintain

89  >  11   Search index = Middle index

Si = 2  Ei =5

Step ni 4, 5 are repeated either end of the list for searching is successful.


Share

About the Author

Akash Padhiyar

Visit Website

There are no comments yet, add one below.

Leave a Comment

Your email address will not be published. Required fields are marked *

*

Time limit is exhausted. Please reload CAPTCHA.