Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Modify the search code given in the lecture for recursive linear search (given below). Write a recursive linear search method that works on sorted data.

Modify the search code given in the lecture for recursive linear search (given below). Write a recursive linear search method that works on sorted data. The method header is:

public static int linearSortedOptimizedRecursiveSearch(Comparable[] data, Comparable target)

Note this is a linear search- not a binary search!

Your code should differ from the provided code in the following ways:

return the index (the location of the element) instead of a boolean

the code will work on Comparable objects (instead of Object)

add optimization so that the linear search for an element that isn't in the array stops once you know that's the case (you can refer to the similar improvement made to the iterative version)

boolean linearSearch(int first, int last, Object[] data, Object target) { if(first > last) { return false; } else if(target.equals(data[first])) { return true; } else { return linearSearch(first+1, last, target, data); } }

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_2

Step: 3

blur-text-image_3

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

Database And Transaction Processing

Authors: Philip M. Lewis, Arthur Bernstein, Michael Kifer

1st Edition

0201708728, 978-0201708721

More Books

Students also viewed these Databases questions

Question

7. Determine what feedback is provided to employees.

Answered: 1 week ago