Question

In: Computer Science

Assume you need to write a Java program that uses a binary search algorithm to search...

Assume you need to write a Java program that uses a binary search algorithm to search a sorted array for a given value.

1. Write a Java pseudocode that uses recursion to accomplish the task. Here is a hint. When you are searching for a particular value in an array, there are two possible outcomes. 1) The value is found and the array index of that value is returned. 2) The value is not found and we return -1. (5 points).

2. Convert the pseudocode in 1 above into a working Java program. Submit the program separately as a java file with the file name MyBinarySearch (6 points).

3. Refer to Figure 3.2 in page 33 of the textbook. What type of process flow would you use when writing the software in parts 1 and 2 above? Justify your answer in at least two sentences. (4 points).

Solutions

Expert Solution

Thanks for the question.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
===========================================================================



In Binary Search we basically ignore half of the elements just after one comparison.

        Compare num with the middle element.
        If num matches with middle element, we return the mid index.
        Else If num is greater than the mid element, then num  can only lie in right half sub array after the mid element. So we recur for right half.
        Else (num  is smaller) recur for the left half.

============================================================================================

// Java implementation of recursive Binary Search
public class BinarySearch {
    // Returns index of num if it is present in arr[l..r], else
    // return -1
    int binarySearch(int arr[], int low, int upper, int num) {
        if (upper >= low) {
            int mid = low + (upper - low) / 2;

            // If the element is present at the middle itself
            if (arr[mid] == num)
                return mid;

            // If element is smaller than mid, then it can only
            // be present in left subarray
            if (arr[mid] > num)
                return binarySearch(arr, low, mid - 1, num);

            // Else the element can only be present in right
            // subarray
            return binarySearch(arr, mid + 1, upper, num);
        }

        // We reach here when element is not present in array
        return -1;
    }

    // Driver method to test above
    public static void main(String args[]) {
        BinarySearch binSearch = new BinarySearch();
        int arr[] = {21, 33, 37, 40, 80, 99, 101, 140, 410};
        int n = arr.length;
        int num = 101;
        int result = binSearch.binarySearch(arr, 0, n - 1, num);
        if (result == -1)
            System.out.println("Element not present");
        else
            System.out.println("Element found at index " + result);
    }
}

======================================================================================


Related Solutions

Write a program in C language that uses a binary search algorithm to guess a number...
Write a program in C language that uses a binary search algorithm to guess a number from 1 to 100. The computer will keep guessing until they get the users number correct.
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an...
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an array. Use number of data N at least more than 10. The function Binary Search should return the index of the search key V.
Binary Search Algorithm a.) Create a Java application that utilizes the "Binary Search Algorithm" presented in...
Binary Search Algorithm a.) Create a Java application that utilizes the "Binary Search Algorithm" presented in chapter 19 (NOT Java's pre-built binarySearch() method from imported Java library) to search for an integer in a random array of size 30 in the range of 0 to 1000 inclusive. You should use Java's random number generator to randomize the numbers in your array. b.) The application's main() method should display unsorted array and sorted array, prompt user for a search key, allow...
Make a Binary search program for C# and write algorithm and explain it in easy words...
Make a Binary search program for C# and write algorithm and explain it in easy words also show output and input
Write a java program that presents the user with the following menu Linear Search Binary Search...
Write a java program that presents the user with the following menu Linear Search Binary Search The user can choose any of these operations using numbers 1 or 2. Once selected, the operation asks the user for an input file to be searched (the file is provided to you and is named input.txt). If option 1 or 2 is selected, it asks the user for the search string. This is the string that the user wants the program to search...
Create a List object that uses the binary search algorithm to search for the string "A"....
Create a List object that uses the binary search algorithm to search for the string "A". Display a message box indicating whether the value was found. Language: C#
Write a JAVA program to modify the insert and remove of a binary search tree to...
Write a JAVA program to modify the insert and remove of a binary search tree to become an AVL tree.
Need in JAVA. You are to use Binary Trees to do this Program. Write a complete...
Need in JAVA. You are to use Binary Trees to do this Program. Write a complete program, which will process several sets of numbers: For each set of numbers you should: 1. Create a binary tree. 2. Print the tree using “inorder”, “preorder”, and “postorder”. 3. Call a method Count which counts the number of nodes in the tree. 4. Call a method Children which prints the number of children each node has. 5. Inset and delete several nodes according...
Write a version of the binary search algorithm that can be used to search a string...
Write a version of the binary search algorithm that can be used to search a string vector object. Also, write a program to test your algorithm. (Use the selection sort algorithm you developed in Programming Exercise 12 to sort the vector.) Your program should prompt the user to input a series of strings, ending the input stream with zzz. The program should then prompt the user to search for a specific string in the list. If the string is found...
In java, Finding the maximum value in a BST (Binary Search Tree). Students need to write...
In java, Finding the maximum value in a BST (Binary Search Tree). Students need to write the code.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT