Question: Java 3. Linear and Binary search. (20 points) Write methods that implement linear and binary search. Whether you use an iterative or recursive solution is

 Java 3. Linear and Binary search. (20 points) Write methods that

Java 3. Linear and Binary search. (20 points) Write methods that implement linear and binary search. Whether you use an iterative or recursive solution is up to you, but an iterative solution may be easier for this problem. Have both methods display the value searched for, and the number of iterations needed to find the value. You will need to add System.out.println() statements just before both return statements for each algorithm. You will also need to add some kind of counter to track iterations. To test this, create an array: (0, 1, 2, 3, 4, 5, 6, 7, 8...) of at least 100 values. Search for the following values in the array, first using linear search, then using binary search. Values: n, where n is the length of the array. (If the array has 100 values, search for the number 100.) Your program output could look like: Testing search with an array of length 100 Testing linear search: Linear search needed 5 iterations to find 5. Linear search needed 10 iterations to find 10. Linear search needed 17 iterations to find 17. Linear search needed 20 iterations to find 20. Linear search needed 25 iterations to find 25. Linear search needed 50 iterations to find 50. Linear search needed 83 iterations to find 83. Linear search needed 100 iterations and FAILED to find 100. Linear search needed 100 iterations and FAILED to find -1. Testing binary search: binary Search needed 4 iterations to find 5. binarySearch needed 7 iterations to find 10. binarySearch needed 4 iterations to find 17. binarySearch needed 5 iterations to find 20. binary Search needed 6 iterations to find 25. binarySearch needed 6 iterations to find 50. binarySearch needed 5 iterations to find 83. binarySearch needed 7 iterations and FAILED to find 100. binarySearch needed 6 iterations and FAILED to find -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!