Question

In: Computer Science

Implement a recursive binary search on an array of 8-byte integers in LEGV8

Implement a recursive binary search on an array of 8-byte integers in LEGV8

Solutions

Expert Solution

import java.util.Arrays;
import java.util.Scanner;
 

public class RecursiveBinarySearch {
 
    public static void main(String args[]) {
 
        int[] sortedArray = new int[]{21, 41, 31, 12, 623, 543, 731, 1898};
        Arrays.sort(sortedArray);
 
        System.out.printf("Searching %d using binary search algorithm in %s %n",
                           31, Arrays.toString(sortedArray));
        binarySearch(sortedArray, 31);
 
        System.out.printf("Finding %d in %s by using recursive binary search 
                           algorithm  %n",
                           37, Arrays.toString(sortedArray));
        binarySearch(sortedArray, 37);
 
        System.out.printf("looking %d in array %s by binary search using 
                           recursion %n",
                          623, Arrays.toString(sortedArray));
        binarySearch(sortedArray, 623);
 
        System.out.printf("Binary Search %d in sorted array %s %n", 1898, 
                          Arrays.toString(sortedArray));
        binarySearch(sortedArray, 1898);
 
    }
 
    
    public static void binarySearch(int[] input, int number) {
        int index = binarySearch(input, number, 0, input.length - 1);
        if (index != -1) {
            System.out.printf("Number %d found at index %d %n", number, index);
        } else {
            System.out.printf("Sorry, number %d not found in array %n", number,
                        index);
        }
    }
 
    
    private static int binarySearch(int[] array, int search, int low, int high) {
        //base case
        if (low > high) {
            return -1;
        }
 
        int mid = (low + high) / 2;
 
        
        if (array[mid] == search) {
            return mid;
        } else if (array[mid] < search) {
            return binarySearch(array, search, mid + 1, high);
        } else {
            // last possibility: a[mid] > x
            return binarySearch(array, search, low, mid - 1);
        }
    }
}

Related Solutions

Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Java String search Design and implement a recursive version of a binary search.  Instead of using a...
Java String search Design and implement a recursive version of a binary search.  Instead of using a loop to repeatedly check for the target value, use calls to a recursive method to check one value at a time.  If the value is not the target, refine the search space and call the method again.  The name to search for is entered by the user, as is the indexes that define the range of viable candidates can be entered by the user (that are...
implement in LEGV8 find the smallest value in an array.
implement in LEGV8 find the smallest value in an array.
Binary Search. Write a MIPS assembly program to perform a binary search on A[10], which is an array of 10 positive integers.
Binary Search. Write a MIPS assembly program to perform a binary search on A[10], which is an array of 10 positive integers. Your program should have a main routine that does the following:(a) Prompt the user to enter all the 10 integers in the array.(b) Prompt the user to enter the number to be searched.(c) Reads the integer values and makes sure it is a positive integer.(d) Prints the index of the integer. If the input is not available in...
Given a minimum unimodal array of integers, run the binary search algorithm to find the minimum...
Given a minimum unimodal array of integers, run the binary search algorithm to find the minimum element. You need to show the initial and the iteration-level values of the left index, right index and middle index as well as your decisions to reduce the search space in each iteration. 42 39 2 6 9 16 20 28 31 34
Given an array storing integers ordered by value, modify the binary search routine to return the...
Given an array storing integers ordered by value, modify the binary search routine to return the position of the integer with the greatest value less than K when K itself does not appear in the array. Return ERROR if the least value in the array is greater than K.
Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort I need it in Java with...
Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort I need it in Java with comments and I need the input file to be placed with Scanner not BufferedReader Please help I need Class River Class CTRiver and Class Driver Class River describes river’s name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong() that returns true if river is above 30 miles...
Q2 - 15 pts) Given a minimum unimodal array of integers, run the binary search algorithm...
Q2 - 15 pts) Given a minimum unimodal array of integers, run the binary search algorithm to find the minimum element. You need to show the initial and the iteration-level values of the left index, right index and middle index as well as your decisions to reduce the search space in each iteration. 0 1 2 3 4 5 6 7 8 9 33 32 27 26 24 22 21 8 7 3 IN C++ PLEASE
Q2 - 15 pts) Given a minimum unimodal array of integers, run the binary search algorithm...
Q2 - 15 pts) Given a minimum unimodal array of integers, run the binary search algorithm to find the minimum element. You need to show the initial and the iteration-level values of the left index, right index and middle index as well as your decisions to reduce the search space in each iteration. 0 1 2 3 4 5 6 7 8 9 33 32 27 26 24 22 21 8 7 3
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT