Question

Finish the following binarySearch method to make it work for an ascending order array. The array...

Finish the following binarySearch method to make it work for an ascending order array. The array list is the searching pool. Find target in the list, if so, return target's index in the array. Otherwise, return -1;

   public static int binarySearch (int [] list, int target)

{

}

Homework Answers

Answer #1

public static int binarySearch (int [] list, int target)
{
int one = 0, other = list.length - 1;

while (one <= other) {
int done = one + (other - one) / 2;
  
if (list[done] == target)
return done;
  
if (list[done] < done)
one = done + 1;
  
else
other = done - 1;
}
  
return -1;
}

// Please up vote

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write recursive method to return true if a given array of integers, named numbers, with n...
Write recursive method to return true if a given array of integers, named numbers, with n occupied positions is sorted in ascending (increasing) order, or returns false otherwise. Array can be empty or not. //PRECONDITION: Varible n denotes the number of occupied positions in the array and must be non-negative. Employee class has method getSalary() that returns employee's salary. // An empty array and an array with single element in it, are sorted. Method isSortedRec must be recursive and returns...
An array is sorted (in ascending order) if each element of the array is less than...
An array is sorted (in ascending order) if each element of the array is less than or equal to the next element. An array of size 0 or 1 is sorted Compare the first two elements of the array; if they are out of order, the array is not sorted; otherwise, check the if the rest of the array is sorted. Write a boolean-valued method named isSorted that accepts an integer array, and the number of elements in the array...
Java Lab Assignment Write a method called convertToString. The method will take an array of chars...
Java Lab Assignment Write a method called convertToString. The method will take an array of chars as an argument and it will return a String. Each index of the array will be part of the each index of the String. method must be well documented using JavaDoc, example: /** * The find method checks if the target in the array * @param String [] a - array of Strings * @param String t - value to be searched for *...
THIS IS JAVA PROGRAMMING Write a method that finds the shortest word in an array of...
THIS IS JAVA PROGRAMMING Write a method that finds the shortest word in an array of String values and returns the length, in characters, of that word. The method should use the following header. public static int minLength(String[] array) Write a test program that prompts the user to enter ten strings, store them in an array and invokes this method to return the shortest length, and displays that value. (A near complete program can be found attached to the dropbox)
Using a loop, complete the following method so that it goes through the array from left...
Using a loop, complete the following method so that it goes through the array from left to right, one element at a time and replaces that element with the sum of itself and the next element in the array (if there is a next element). For example, if the input is {1, 3, 5} the output must be {4, 8, 5} -- on the first iteration 1 was replaced by 1+3; on the second iteration 3 was replaced by 3+5....
Finish the code wherever it says TODO /**    * Node type for this list. Each...
Finish the code wherever it says TODO /**    * Node type for this list. Each node holds a maximum of nodeSize elements in an    * array. Empty slots are null.    */    private class Node {        /**        * Array of actual data elements.        */        // Unchecked warning unavoidable.        public E[] data = (E[]) new Comparable[nodeSize];        /**        * Link to next node.       ...
Do a trace on the binarySearch method below: variable key holds the value 17, and variable...
Do a trace on the binarySearch method below: variable key holds the value 17, and variable list is a reference to an array with these values {9, 20, 23, 28, 33, 38, 42, 48, 54, 61,73}. public static int binarySearch(int[] list, int key) {     int lowIndex = 0;     int highIndex = list.length - 1;     while (highIndex >= lowIndex) {       int midIndex = (lowIndex + highIndex) / 2;       if (key < list[midIndex]){            highIndex = midIndex...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
The following program creates a linked list which contains 5 links. Add a method called doubleValue()...
The following program creates a linked list which contains 5 links. Add a method called doubleValue() to the LinkedList class. The doubleValue() method must double the value of the number data field in each link. Add the required code to the main() method to call the doubleValue() method and display the revised list. public class Link { private int number; private Link next;      public Link(int x) { number = x; }    public void displayLink() { System.out.println("The number is:...
The following program creates a linked list which contains 5 links. Add a method called doubleValue()...
The following program creates a linked list which contains 5 links. Add a method called doubleValue() to the LinkedList class. The doubleValue() method must double the value of the number data field in each link. Add the required code to the main() method to call the doubleValue() method and display the revised list. public class Link { private int number; private Link next;      public Link(int x) { number = x; }    public void displayLink() { System.out.println("The number is:...