Question

In: Computer Science

20.14 Program BinarySearch Objectives Examine a binary search algorithm Debug existing code Instructions For this lab...

20.14 Program BinarySearch

Objectives

  • Examine a binary search algorithm
  • Debug existing code

Instructions

For this lab you will code directly in ZyBooks. That means no uploading a file. If you wish, you can copy the template code into your IDE, work out a solution, and paste that into the code window.

The problem
The code does not work on certain data sets. Fix the sets but do not alter the binary search algorithm.

The obvious
Yes, this is a problem that can be solved with one additional statement. It is the last third of the semester and don't you want a break??

Program BinarySearch

/**
* Binary search to fix data sets issues
*/
import java.util.Scanner;

public class BinarySearch {
/** Use binary search to find the key in the list */
public static int binarySearch(int[] list, int key) {
int low = 0;
int high = list.length - 1;

while (high >= low) {
int mid = (low + high) / 2;
if (key < list[mid])
high = mid - 1;
else if (key == list[mid])
return mid;
else
low = mid + 1;
}

return -low - 1;
}
  
public static void main(String[] args) {

Scanner in = new Scanner(System.in);

int set = in.nextInt();
int key = in.nextInt();

int[][] datasets = { {},
{1,2,3,5,8,13,21,34,55,89},
{-81, -72, -63, -54, -45, -36, -27, -18, -9, 0},
{21, 34, 72, -63, 8, 5, -13, -27, -18, 1, 0, 2}
};
  
System.out.println("Searching for key " + key +
" in data set " + set +
" returned " + binarySearch(datasets[set], key));

}
}

Run your program as often as you'd like, before submitting for grading. Below, type any needed input values in the first box, then click Run program and observe the program's output in the second box.

Solutions

Expert Solution

For binary search the input array must be sorted. Here a statement is added to sort the array.

If you need any corrections kindly comment.

Program

/**
* Binary search to fix data sets issues
*/
import java.util.Scanner;
import java.util.Arrays;
public class BinarySearch {
/** Use binary search to find the key in the list */
public static int binarySearch(int[] list, int key) {
int low = 0;
int high = list.length - 1;

while (high >= low) {
int mid = (low + high) / 2;
if (key < list[mid])
high = mid - 1;
else if (key == list[mid])
return mid;
else
low = mid + 1;
}

return -low - 1;
}

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

int set = in.nextInt();
int key = in.nextInt();

int[][] datasets = { {},
{1,2,3,5,8,13,21,34,55,89},
{-81, -72, -63, -54, -45, -36, -27, -18, -9, 0},
{21, 34, 72, -63, 8, 5, -13, -27, -18, 1, 0, 2}
};
Arrays.sort(datasets[set]);//Sort the set before calling binary search
System.out.println("Searching for key " + key +
" in data set " + set +
" returned " + binarySearch(datasets[set], key));

}
}

Output

user@user-Lenovo-V330-15IKB:~/JavaPrograms$ java BinarySearch
3
-27
Searching for key -27 in data set 3 returned 1


Related Solutions

Given the pseudocode for Binary Search Algorithm as below: BinarySearch(A, p, r, V)    if p...
Given the pseudocode for Binary Search Algorithm as below: BinarySearch(A, p, r, V)    if p < r q = (p + r)/2 if V = A[q] return q else if V > A[q] return BinarySearch(A, q+1, r, V)    else return BinarySearch(A, p, q-1) else if p = r,    if V = A[p]    return p else return -1    return -1 end function Using this pseudocode, write a function for BinarySearch and also complete the program, by...
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...
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...
Into to PythonInstructions: • In this lab you are to debug the code found at...
Into to PythonInstructions: • In this lab you are to debug the code found at the end of these instructions: Find and correct all problems found in the code. Create comments in the code explaining what you found and how you corrected it • The purpose of this program is to calculate car insurance rates using the following rules: The base rate of insurance is $50 a month, Males pay a 25% premium over the base rate, Drivers in Michigan...
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.
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 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.
Correct this Binary Search (C++) // This program demostrates linear search algorithm #include <iostream> using namespace...
Correct this Binary Search (C++) // This program demostrates linear search algorithm #include <iostream> using namespace std; // Binary search algorith // f is the first , l is the last , t is the target int binarySearch(int stgrade[], int f, int l, int t) { while (f <= l) { int m = f + (l - l) / 2; // Check if x is present at mid if (stgrade[m] == t) return m; // If x greater, ignore...
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 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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT