Binary Search Test

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;

}

}

 

Advertisements

About qainterviews

Software Geek,QA Expert,Blogger
This entry was posted in Data Structures, Java, Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s