Question

JAVA Algorithms course need help completing this method using binary search thank you in advance. -------------------------------------------------------------------------------------------------...

JAVA Algorithms course need help completing this method using binary search thank you in advance.

-------------------------------------------------------------------------------------------------

/**
A class for executing binary searches in an array.
*/
public class BinarySearcher
{
/**
Finds a value in a range of a sorted array, using the binary
search algorithm.
@param a the array in which to search
@param low the low index of the range
@param high the high index of the range
@param value the value to find
@return the index at which the value occurs, or -1
if it does not occur in the array
*/
public static int search(int[] a, int low, int high, int value)
{
     
}


0 0
Add a comment Improve this question Transcribed image text
Answer #1
/**
 A class for executing binary searches in an array.
 */
public class BinarySearcher {
  /**
   * Finds a value in a range of a sorted array, using the binary
   * search algorithm.
   *
   * @param a     the array in which to search
   * @param low   the low index of the range
   * @param high  the high index of the range
   * @param value the value to find
   * @return the index at which the value occurs, or -1
   * if it does not occur in the array
   */
  public static int search(int[] a, int low, int high, int value) {
    int mid;
    if(low<=high){
      mid = (low+high)/2;
      if(a[mid] == value){
        return mid;
      }
      else if(a[mid] < value){
        return search(a,mid+1,high,value);
      }
      else{
        return search(a,low,mid-1,value);
      }
    }
    return -1;
  }
}

Add a comment
Know the answer?
Add Answer to:
JAVA Algorithms course need help completing this method using binary search thank you in advance. -------------------------------------------------------------------------------------------------...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need help with this Java project implementing an interpolation search, bolded the missing parts: /**   ...

    Need help with this Java project implementing an interpolation search, bolded the missing parts: /**    A class that implements an interpolation search and    a binary search of a sorted array of integers.    @author Frank M. Carrano    @author Timothy M. Henry    @version 4.0 */ public class SearchComparer {    private int[] a;    private int interpolationSearchCounter;    private int binarySearchCounter;    private static final int CAPACITY = 50;    public SearchComparer(int[] array, int n)    {...

  • Write a Java application that implements the recursive Binary Search alogrithm below: Write a Java application...

    Write a Java application that implements the recursive Binary Search alogrithm below: Write a Java application that implements the recursive Binary Search alogrithm below:/** * Performs the standard binary search using two comparisons * per level. This is a driver that calls the recursive method * @return index where item is found or NOT FOUND if not found */public static <AnyType extends Comparable<? super AnyType>> int binarySearch(AnyType [] a, AnyType x) {return binarySearch(a, x, 0, a.length -1);}/** * Hidden recursive...

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • For this lab, you will be writing search algorithms. I have provided method signatures for you...

    For this lab, you will be writing search algorithms. I have provided method signatures for you to follow. The code I am providing already compiles, so as you write more code you can continually test it as you go. Write all of the methods in order as they appear in the code. DO NOT move on to another method until the previous method is fully tested. 1. The CandyBar class defines an object of type CandyBar. This object has three...

  • Searching/sorting tasks and efficiency analysis - Binary Search Search for the character S using the binary...

    Searching/sorting tasks and efficiency analysis - Binary Search Search for the character S using the binary search algorithm on the following array of characters: A E G K M O R S Z. For each iteration of binary search use a table similar to the table below to list: (a) the left index and (b) the right index of the array that denote the region of the array that is still being searched, (c) the middle point of the array,...

  • Java: Write an application that has an array of at least 20 integers. It should call...

    Java: Write an application that has an array of at least 20 integers. It should call a method that uses the sequential search algorithm to locate one of the values. The method should keep a count of the number of comparisons it makes until it finds the value. Then the program should call another method that uses the binary search algorithm to locate the same value. It should also keep count of the number of comparisons it makes. Display these...

  • I am currently using eclipse to write in java. A snapshot of the output would be...

    I am currently using eclipse to write in java. A snapshot of the output would be greatly appreciated to verify that the program is indeed working. Thanks in advance for both your time and effort. Here is the previous exercise code: /////////////////////////////////////////////////////Main /******************************************* * Week 5 lab - exercise 1 and exercise 2: * * ArrayList class with search algorithms * ********************************************/ import java.util.*; /** * Class to test sequential search, sorted search, and binary search algorithms * implemented in...

  • JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration...

    JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration skill Problem description: Write the following eight methods and write a main function to test these methods // return the index of the first occurrence of key in arr // if key is not found in arra, return -1 public static int linearSearch(int arr[], int key) // sort the arr from least to largest by using select sort algorithm public stati void selectSort(int arr[])...

  • I need the following merge-sort assignment completed using the "ArrayList<Comparable>" (the part I'm really stuck on)...

    I need the following merge-sort assignment completed using the "ArrayList<Comparable>" (the part I'm really stuck on) with comments: import java.util.ArrayList; public class Mergesort { /** * Sorts list given using the mergesort algorithm * @param list the list to be sorted * @param first the index of the first element of the list to be sorted * @param last the index of the last element of the list to be sorted */ public static void mergesort(ArrayList<Comparable> list, int first, int...

  • JAVA question: Suppose we are performing a binary search on a sorted array called numbers initialized...

    JAVA question: Suppose we are performing a binary search on a sorted array called numbers initialized as follows: // index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 int[] numbers = {-30, -9, -6, -4, -2, -1, 0, 2, 4, 10, 12, 17, 22, 30}; ​ // search for the value -5 int index = binarySearch(numbers, -5); Write the indexes of the elements that would be examined by the binary search (the mid values...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT