Question

In: Computer Science

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 user to enter a search value from the keyboard, read in the user's keyboard entry, then perform binary search for the search key in the array after the array is sorted, then inform the user of the search result in the console by displaying a message indicating whether or not the value is found, if found, also display its index in the array. Note:binarySearch() for a value in an array require the array to be sorted first, before searching for any value in the array.

Solutions

Expert Solution

Here is your program. Hope it helps. upvoting would be really helpfu Thanks :)

import java.io.*;
import java.util.Random;
import java.util.Arrays;
import java.util.*;
public class hello{
   public static void main(String[] args) {
       int up =100;
      Random rd = new Random(); // creating Random object
      int[] arr = new int[30];
      int low = 0;
      int high = 1000,result = 0;
      
      for (int i = 0; i < arr.length; i++) {
       
        arr[i] = rd.nextInt(high-low) + low;
        
         // storing random integers in an array
         // printing each array element
         
      }
      System.out.println("unSorted array is ");
      for (int i = 0; i < arr.length; i++) {
          System.out.println(arr[i]);
      }
      Arrays.sort(arr);
      System.out.println("Sorted array is ");
       for (int i = 0; i < arr.length; i++) {
       
         System.out.println(arr[i]);}
      Scanner sc = new Scanner(System.in);
      int n;
      System.out.println("Enter Search Element");
      n = sc.nextInt();
     
      
      int mid = 0,l = 0,hi = 29,k=0;
      while(l<=hi)
      {
          mid = (l+hi)/2;
          if(arr[mid] == n)
          {
              k = 1;
              break;
              
          }
          else if(arr[mid]<n)
          {
            l = mid+1;  
          }
          else if(arr[mid]>n)
          
          hi = mid-1;
      }
      if(k==1)
      System.out.println("Success");
      else
      System.out.println("Fail");
      
   }
}

Related Solutions

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#
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...
Binary Tree Create a binary search tree using the given numbers in the order they’re presented....
Binary Tree Create a binary search tree using the given numbers in the order they’re presented. State if the resulting tree is FULL and/or BALANCED. 37, 20, 18, 56, 40, 42, 12, 5, 6, 77, 20, 54
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...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20 unsorted numbers. You should initiate this array yourself and first output the array in its original order, then output the array after it has been sorted by the selection sort algorithm. Create a second Java Application that implements an Insertion sort algorithm to sort the same array. Again, output the array in its original order, then output the array after it has been sorted...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20 unsorted numbers. You should initiate this array yourself and first output the array in its original order, then output the array after it has been sorted by the selection sort algorithm.
JAVA PROGRAM Create a Binary Search Tree with the following elements in the order mentioned below:...
JAVA PROGRAM Create a Binary Search Tree with the following elements in the order mentioned below: 5, 85, 89, 3, 2, 8, 65, 92 Print the Pre-order of this tree Print the height and the balance factor of the nodes in the order they were inserted (5, 85, 89, 3, 2, 8, 65, 92) in the form of a table with three columns and 9 rows. Use column headers “Node”, “Height”, and “Balance Factor” for the three columns respectively. Use...
In java, create a binary search tree (without using additional libraries) of stacks where each node...
In java, create a binary search tree (without using additional libraries) of stacks where each node contains a key and a stack. In this binary search tree, when adding a new element to the tree it is inserted based off of its key value; if that key value already exist then the element is pushed into the stack at that location, if the key value does not exist a node element is added to the tree to reflect that key,...
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...
(a) Consider the general k-ary search algorithm (generalization of binary search) which splits a sorted array...
(a) Consider the general k-ary search algorithm (generalization of binary search) which splits a sorted array of size n into k subsets each of size n/k and recursively searches only one of these k subsets. Which one of these k subsets should be searched is decided by making (k − 1) comparisons, each of which can be done in constant time. Clearly, the recurrence relation for this k-ary search algorithm can be written as, T(n) = T(n/k) + (k −...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT