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...
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#
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the template provided under the PolyLearn assignment, using the class TreeNode that is also provided. You may (should) implement helper methods that make your code easier to write, read, and understand. You will also need to write test cases of your own as you develop the methods. You may use iterative and/or recursive functions in your implementation. The following starter files are available . •...
// This program demonstrates a Binary Search, which search for a value // in an array,...
// This program demonstrates a Binary Search, which search for a value // in an array, assuming that the array is sorted in descending order. // You have to modify the function binarySearch() to search for a value // in an array that is sorted in ascending order. // NOTES: // Uncomment line 34 and comment line 32. You don't have to edit anything // else in the main(), just in the binarySearch() function. // EXAMPLES (using the array sorted...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT