1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
sergij07 [2.7K]
3 years ago
6

Write a recursive method named binarySearch that accepts a sorted array of integers and an integer target value and uses a recur

sive binary search algorithm to find and return an index at which that target value is found in the array. If the target value is not found in the array, return -1. The following code shows some example calls and their expected return values: // index 0 1 2 3 4 5
Computers and Technology
1 answer:
djyliett [7]3 years ago
6 0

Answer:

10

-1

Process finished with exit code 0

Explanation: see code bellow

public class BinarySearchRecursive {

   public static int binarySearch(int[] a, int key) {

       return binarySearchRecursiveHelper(a, key, 0, a.length - 1);

   }

   public static int binarySearchRecursiveHelper(int[] arr, int target, int start, int end) {

       if (start > end) {

           return -1;

       }

       int mid = (start + end) / 2;

       if (arr[mid] == target) {

           return mid;

       } else if (target < arr[mid]) {

           return binarySearchRecursiveHelper(arr, target, start, mid - 1);

       } else {

           return binarySearchRecursiveHelper(arr, target, mid + 1, end);

       }

   }

   public static void main(String[] args) {

       int[] a = {-4, 2, 7, 10, 15, 20, 22, 25, 30, 36, 42, 50, 56, 68, 85, 92, 103};

       int index = binarySearch(a, 42);   // 10

       System.out.println(index);

       index = binarySearch(a, 66);   // -1

       System.out.println(index);

   }

}

You might be interested in
What sentence could Lou’s granddaughter not answer? Explain the context and discuss whether you could answer this question or no
Tanya [424]
He wouldn’t answer the context for the discussion
7 0
2 years ago
Which view allows you to make changes to the content of your presentation?
Neporo4naja [7]

Answer:

Share your screen and edits the parts you want. What meet are you using, zoom or something else?

6 0
3 years ago
1. Create an interface called Runner. The interface has an abstract method called run() that display a message describing the me
Colt1911 [192]

Answer:

see explaination for program code

Explanation:

interface Runner

{

public abstract void run();

}

class Machine implements Runner

{

public void run()

{

System.out.println("Machine is running");

}

}

class Athlete implements Runner

{

public void run()

{

System.out.println("Athlete is running");

}

}

class PoliticalCandidate implements Runner

{

public void run()

{

System.out.println("Political Candidate is running");

}

}

class DemoRunners

{

public static void main (String[] args)

{

Machine m = new Machine();

m.run();

Athlete a = new Athlete();

a.run();

PoliticalCandidate pc = new PoliticalCandidate();

pc.run();

}

}

7 0
4 years ago
T or F: Is there such thing as an OptiPlex 3020
eduard
T.
I hope this helps!
8 0
3 years ago
Businesses around the world all need access to the same data, so there
Alexxx [7]

Answer:

B

Explanation:

3 0
2 years ago
Other questions:
  • Using a windows computer, to expand a branch of the folder tree you would _____.
    14·1 answer
  • Esther has acquired an associate's degree in information technology and certifications in PageMaker and Illustrator. Which caree
    8·2 answers
  • Consider this data sequence: "fish bird reptile reptile bird bird bird mammal fish". let's define a singleton to be a data eleme
    5·1 answer
  • What should be included as part of the approval process? in relation to computer forensics
    14·1 answer
  • Write a program to randomly fill an array of float of 10 elements (use the rand() function and make sure to use #include ) in ma
    11·1 answer
  • Your team will write a function that reverses a C-string, to practice using pointers. You must use pointers and C-strings for th
    6·1 answer
  • A human subject’s photographs show two catchlights in each eye that are unwanted by the photographer. What is the most likely ca
    12·1 answer
  • Choose all items that represent examples of good website accessibility.
    8·2 answers
  • B. Differentiate Operation System from other software. give me 4-5 sentences pls.
    10·2 answers
  • What steps can you take to secure your private information?.
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!