Question

In: Computer Science

Write a method in JAVA, called binarySearch, that returns the index of the key element if...

  1. Write a method in JAVA, called binarySearch, that returns the index of the key element if found in the array, otherwise it will return the value of minus(insertion point +1).
  2. In main, use an initializer list create an array of ints called nums holding the following values: 1, 4, 13, 43, -25, 17, 22, -37, 29. Test your binarySearch method on nums with a key value number in it (e.g. 4) and one that is not (e.g. 100).
  3. Ex. Given the array values {1, 4, 4, 22, -5, 10, 21, -47, 23}, the array will be sorted as -47, -5, 1, 4, 4, 10, 21, 22, 23. The index for the key value of 4 will be 3. The return value when searching for 6 will be the insertion point for 6 which is index of 5, so the return will be minus(5+1) = -6.
  4. Create an int array of size 20, called data and write a method to fill the array with random ints [-100, 100]. Recall that random’s nextInt( x) method returns a value from 0 to x-1. The upper bound is exclusive. Modify the values to fit the required ranges.
  5. Use the binarySearch method you previously wrote to return the index of the key value searched for in data for the client to print. Let’s use 10 as the key value for every run. Notice that 10 may not be part of the data array every run.
  6. Print the value of the key value searched for (10) and the index of the value, which is the value of -(insertion point +1).

Solutions

Expert Solution

//Java program

import java.util.Random;

public class BinarySearch {
   public static void bubbleSort(int a[],int n){
       for(int i=n-1;i>=0;i--){
           for(int j=0;j<i;j++){
               if(a[j]>a[j+1]){
                   int temp=a[j];
                   a[j]=a[j+1];
                   a[j+1]=temp;
               }
           }
       }
   }
   public static int binarySearch(int a[], int key, int low, int high)
   {
   if (high <= low)
   return ((key > a[low])? (low + 1): low) +1;
   int mid = (low + high)/2;
   if(key == a[mid]) return mid;
  
   if(key > a[mid]) return binarySearch(a, key, mid+1, high);
  
   return binarySearch(a, key, low, mid-1);
   }
  
   public static void main(String args[]) {
       int arr[] = {1, 4, 13, 43, -25, 17, 22, -37, 29};
       bubbleSort(arr,arr.length);
       System.out.println("Index of 4 : "+binarySearch(arr,4,0,arr.length-1));
       System.out.println("Index of 100 : "+binarySearch(arr,100,0,arr.length-1));
      
       int arr1[] = {1, 4, 4, 22, -5, 10, 21, -47, 23};
       bubbleSort(arr1,arr1.length);
       System.out.println("\n\nIndex of 4 : "+binarySearch(arr1,4,0,arr1.length-1));
       System.out.println("Index of 100 : "+binarySearch(arr1,6,0,arr1.length-1));
      
       int arr2[] = new int[20];
       Random random = new Random();
       for(int i=0;i<20;i++) {
           arr2[i] = -100+random.nextInt(200);
       }
       bubbleSort(arr2,arr2.length);
       System.out.println("\n\nIndex of 10 : "+binarySearch(arr2,4,0,arr2.length-1));
      
   }
}
//sample output


Related Solutions

Write a Java method that returns the index of the largest element in an array of...
Write a Java method that returns the index of the largest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: 
 public static int indexOfLargestElement(double[] array)
 Write a test program that prompts the user to enter ten numbers, invokes this
method to return the index of the largest element, and displays the index.
Using Java Write a method that returns the index of the smallest element in an array...
Using Java Write a method that returns the index of the smallest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header:   public static int indexOfSmallestElement (double[] array)
Write a method called mode that returns the most frequently occurring element of an array of...
Write a method called mode that returns the most frequently occurring element of an array of integers. Assume that the array has at least one element and that every element in the array has a value between 0 and 100 inclusive. Break ties by choosing the lower value. For example, if the array passed contains the values [27, 15, 15, 11, 27], your method should return 15. write a version of this method that does not rely on the values...
Code with Java and no imports, Method sandwiched returns true if num is in the element...
Code with Java and no imports, Method sandwiched returns true if num is in the element before and after an element that is not equal to num sandwiched([4,5,4,6,7,3], 4) returns true sandwiched([2,1,2], 2) returns true sandwiched([3,3,3], 3) returns false sandwiched([4,5,6,4], 4) returns false sandwiched([1,1,2,3,1,4,1], 1) returns true @param nums Integer ArrayList @param num integer @return true if a single number is between elements equal to num */ public static boolean sandwiched(ArrayList nums, int num) { return false; }//end sandwiched
write a method that returns the index of the second smallest element in an array of integers. If the number of such elements is greater than 1.
write a method that returns the index of the second smallest element in an array of integers. If the number of such elements is greater than 1. return the second smallest index. Use the following header:public static int index of seconds sma11eststenent tint array
IN JAVA Write a program with a method that returns an array. The method should accept...
IN JAVA Write a program with a method that returns an array. The method should accept as input a comma-delimited string with three values from a user. The array should store each value in a different element. Use Try..Catch error handling and print any failure messages, or print success from within method if the execution is successful (see Chapter 13 in the text). Call the method from the main method of the program to demonstrate its functionality by looping through...
Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called...
Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called without instantiating the class and returns a random Date between Jan 1, 2000 and Dec 31, 2010.
write a method called Comparisons that will return the number of comparisons to find an element...
write a method called Comparisons that will return the number of comparisons to find an element in the tree. The main program calls this method for each element, adding the comparisons each time in order to count the total number of comparisons. The program then outputs the total number of comparisons and the average number. You may use the program BuildTreeWIthMethod to build your tree. Then, after you have made the call to inOrder(root), add the following code: int totalComparisons=0;...
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
In JAVA!!! write a method called Comparisons that will return the number of comparisons to find...
In JAVA!!! write a method called Comparisons that will return the number of comparisons to find an element in the tree. The main program calls this method for each element, adding the comparisons each time in order to count the total number of comparisons. The program then outputs the total number of comparisons and the average number. You may use the program BuildTreeWIthMethod to build your tree. Then, after you have made the call to inOrder(root), add the following code:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT