Question: in java Background: Applying linear search and binary search on arrays. Step 1: Search class Implement a Search class that will contain methods for linear

in java

Background: Applying linear search and binary search on arrays.

Step 1: Search class

Implement a Search class that will contain methods for linear search and binary search. Implement 2 linear search methods (one for integers and one for Strings). Also, implement 2 binary search methods (one for integers and one for Strings). The methods will take 2 parameters: an array and a search item, and will return the location of the item in the array if found, or -1 if not found.

Search Class

Methods:

public static int linearSearch( int[] values, int searchValue)

public static int linearSearch( String[] words, String searchWord)

public static int linearSearch( int[] values, int searchValue)

public static int linearSearch( String[] words, String searchWord)

Sample code for linear search and binary search (integer versions) is given below. Both examples work on a sample array of integers called values, and an integer parameter variable called searchValue.

Linear Search

for(inti=0;i

if(values[i]==searchValue){

returni;

}

}

return 1;

Binary Search

booleanfound=false;

intlow=0,pos=0,high=values.length 1;

while(low<=high&&!found){

pos=(low+high)/2;//Midpoint of the subsequence

if (values[pos]==searchValue){

found=true;}. //Found it!

else if(values[pos] < searchValue){

low=pos+1;}. //. Look in second half

else{

high=pos 1;}. // Look in first half

}

if(found){

return pos; }

else{

return 1;}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!