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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started