Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me add more code in the code below so that it fulfills the requirement. Especially the last one if the input are: 8

Please help me add more code in the code below so that it fulfills the requirement. Especially the last one if the input are: 8 10 15 20 25 30 35 40 45 50 --> The output are: index: -1, recursions: 4, comparisons: 7. I have posted this question many times and I received many results wrong from experts with the final case input 8 10 15 20 25 30 35 40 45 50 , and the result from experts always have: index: -1, recursions: 5, comparisons: 8 instate of index: -1, recursions: 4, comparisons: 7. Thank you very much.

if the input are:9 1 2 3 4 5 6 7 8 9 2 --> Output are : index: 1, recursions: 2, comparisons: 3

if the input are: 9 11 22 33 44 55 66 77 88 99 11 --> The output are: index: 0, recursions: 3, comparisons: 5

if the input are: 9 11 22 33 44 55 66 77 88 99 99 --> The output are: index: 8, recursions: 4, comparisons: 7 .

if the input are: 8 10 15 20 25 30 35 40 45 50 --> The output are: index: -1, recursions: 4, comparisons: 7

And final requirement: Test binarySearch() with [10, 20, 20, 20, 20, 20, 25, 30, 35, 40, 45, 50, 60] and target 20. Should return 2.

Thank you very much! I really need the result correct with exactly output above if the input like above! Thank you.

This is the base code in Java that need to be added:

import java.util.Scanner; import java.util.ArrayList; import java.util.Collections;

public class LabProgram { // Read and return an ArrayList of integers. private static ArrayList readNums(Scanner scnr) { int size = scnr.nextInt(); // Read size of ArrayList ArrayList nums = new ArrayList(); for (int i = 0; i < size; ++i) { // Read the numbers nums.add(scnr.nextInt()); } return nums; }

static public int binarySearch(int target, ArrayList integers, int lower, int upper) { /* Type your code here. */ }

public static void main(String [] args) { Scanner scnr = new Scanner(System.in); // Input a list of integers ArrayList integers = readNums(scnr);

// Input a target value for the search int target = scnr.nextInt();

int index = binarySearch(target, integers, 0, integers.size() - 1);

System.out.printf("index: %d, recursions: %d, comparisons: %d ", index, recursions, comparisons); } }

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

Secrets Of Analytical Leaders Insights From Information Insiders

Authors: Wayne Eckerson

1st Edition

1935504347, 9781935504344

More Books

Students also viewed these Databases questions