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...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns true if the String is a palindrome.
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
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 function that takes a valid stringlist and returns the index of the smallest element...
Write a function that takes a valid stringlist and returns the index of the smallest element in the list represented by the stringlist. You may not use split(). Examples: >>> stringlist min index('[123,53,1,8]') # 1 is smallest2 >>> stringlist min index('[1,2,345,0]') # 0 is smallest3 >>> stringlist min index('[5] ') # 5 is smallest0
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...
Problem Description: Write a method that returns the smallest element in a specified column in a...
Problem Description: Write a method that returns the smallest element in a specified column in a matrix using the following header: public static double columnMin(double[][] m, int columnIndex) Write a program that reads from the keyboard the number of rows and columns, and an array of double floating point numbers with the specified number of rows and columns. The program will then invoke the columnMin method and displays the minimum value of all columns.
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT