Binary Search

package Prog;

public class BinarySearch
{
  static int binarySearch(int a[], int beg, int end, int item)
{
     int mid;
     while(end >= beg) 
     {   
         mid = (beg + end)/2;
         if(a[mid] == item)
         {
             return mid+1;
         }
         else if(a[mid] < item) 
         {
             beg = mid + 1;
         }
         else 
         {
             end = mid - 1; 
         }
     
     }
     return -1; 

public static void main(String args[]){
        int arr[] = {10,20,30,40,50,60};
        int key = 30;
        int last=arr.length-1;
        if (binarySearch(arr,0,last,key)>0)
        System.out.println("item found at location ="+ binarySearch(arr,0,last,key));
        else        
        System.out.println("item not found");
}

}

output:
item found at location =3

Post a Comment

6 Comments

  1. Steve is a successful business owner who takes his business very seriously. He focuses on growing his business and has several employees. People love his products and services and are sharing them with others. What Steve is struggling with is making his business financially fit. It seems like his business is always tight, and he is barely making it each month. Sound familiar? Local Business Coupons New York

    ReplyDelete
  2. Hello, this weekend is good for me, since this time i am reading this enormous informative article here at my home. Best Forex Signal Service

    ReplyDelete
  3. I would like to say that this blog really convinced me to do it! Thanks, very good post. pocket Option Review

    ReplyDelete
  4. I know this is one of the most meaningful information for me. And I'm animated reading your article. But should remark on some general things, the website style is perfect; the articles are great. Thanks for the ton of tangible and attainable help. wallstreet forex robot 2.0

    ReplyDelete
  5. Keep up the good work; I read few posts on this website, including I consider that your blog is fascinating and has sets of the fantastic piece of information. Thanks for your valuable efforts. best 60 second binary options brokers

    ReplyDelete
Emoji
(y)
:)
:(
hihi
:-)
:D
=D
:-d
;(
;-(
@-)
:P
:o
:>)
(o)
:p
(p)
:-s
(m)
8-)
:-t
:-b
b-(
:-#
=p~
x-)
(k)