Go to Top

Sunday 25 September 2011

Binary search

Java Program for Binary Search : 


class bsearch
{
 static void search(int[]a,int key)
 {
     int p=-1,h=a.length-1,l=0,m;
     while(p==-1 && h>=l)
     {
         m=(h+l)/2;
         if(a[m]>key)
         h=m-1;
         if(a[m]<key)
         l=m+1;
         if(a[m]==key)
         p=m;
        }
     if(p==-1)
     System.out.print("Search key '"+key+"' not found");
     else
     System.out.print("Search key '"+key+"' found at : Position "+(p+1));
    }
}

1 comment:

  1. HI,

    I want help in JAVA
    if you read this comment

    then contact me at amaanrizwan007@gmail.com

    ReplyDelete

ShareThis