Binary search program.



Posted by Dr. jha
Link-->To his blog.



class Search{

public static void main(String args[]){
int a[]={40,50,20,47,28,16,60,8,70};

Bindu B = new Bindu();

B.B_search(a,70);
B.bubble(a);
for(int i=0;i
System.out.println("soretd array"+a[i]);
}

}
}
class Bindu{


void B_search(int ar[],int key){

System.out.println(ar.length + ":" + key);
int low=0,high=ar.length,mid=0;

while(low<=high){

mid=(low+high)/2;
if(ar[mid]==key){
System.out.println("required no is"+ar[mid]);
break;
}
else
{
if(ar[mid] low=mid+1;
else
high=mid-1;
}

}

}




void bubble(int ar1[]){
int i=0,j=0;
for(i=(ar1.length-1);i>0;i--){
for(j=0;j if(ar1[i]>ar1[i+1]){
int temp=ar1[i];
ar1[i]=ar1[i+1];
ar1[i+1]=temp;
}
}
}
}

}



/*void Rec_search(int a[],int high,int low,int key){

int mid=high+low;
if(a[mid]==key)
return a[mid];
else{
if(a[mid] low=mid+1;
else
high=mid+1;
}





}*/