import org.testng.annotations.Test;

/**

* Binary Search Test

* @author asharda

*

*/

public class BSearchTest {

/**

* Check if Number is Present in Array

*/

@Test

public void testBinarySearch()

{

int arr[]={1,5,7,8,12,19};

boolean b=bSearch(arr,19);

if(b==true)

{

System.out.println(“Number Found”);

}

else

{

System.out.println(“Number Not Found”);

}

}

/**

* Return if Number Found

* @param arr

* @param target

* @return

*/

public boolean bSearch(int arr[],int target)

{

int low=0;

int high=arr.length-1;

int mid;

while(low<=high)

{

mid=low+high/2;

if(target==arr[mid])

{

return true;

}

else if(target>arr[mid])

{

low=low+1;

}

else if(target<arr[mid])

{

high=high-1;

}

}

return false;

}

}

### Like this:

Like Loading...

*Related*

## About qainterviews

Software Geek,QA Expert,Blogger