Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Databases Technologies And Applications

Authors: Zongmin Ma

1st Edition

1599041219, 978-1599041216

More Books

Students also viewed these Databases questions

Question

What are Measures in OLAP Cubes?

Answered: 1 week ago

Question

How do OLAP Databases provide for Drilling Down into data?

Answered: 1 week ago

Question

How are OLAP Cubes different from Production Relational Databases?

Answered: 1 week ago