Create a Java application that implement "Binary Search Algorithm" presented in chapter 19 (NOT by using Java's pre-built binarySearch() method from imported Java library object) to search for an integer in an array of random integers of size 50 (e.g. 50 random integers) in the range 0 to 2000 inclusive. You should use Java's random number generator method to randomize the numbers in your random integer array. 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: binary search for a value in an array require the array to be sorted in advance, before searching for any value in the array.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Code Plain Text:

// Fig. 19.3: BinarySearchTest.java
// Use binary search to locate an item in an array.
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Scanner;

public class BinarySearchTest {
// perform a binary search on the data
public static int binarySearch(int[] data, int key) {
int low = 0; // low end of the search area
int high = data.length - 1; // high end of the search area
int middle = (low + high + 1) / 2; // middle element
int location = -1; // return value; -1 if not found

do { // loop to search for element
// print remaining elements of array
System.out.print(remainingElements(data, low, high));

// output spaces for alignment
for (int i = 0; i < middle; i++) {
System.out.print(" ");
}
System.out.println(" * "); // indicate current middle

// if the element is found at the middle
if (key == data[middle]) {
location = middle; // location is the current middle
}
else if (key < data[middle]) { // middle element is too high
high = middle - 1; // eliminate the higher half
}
else { // middle element is too low
low = middle + 1; // eliminate the lower half
}

middle = (low + high + 1) / 2; // recalculate the middle
} while ((low <= high) && (location == -1));

return location; // return location of search key
}

// method to output certain values in array
private static String remainingElements(
int[] data, int low, int high) {
StringBuilder temporary = new StringBuilder();

// append spaces for alignment
for (int i = 0; i < low; i++) {
temporary.append(" ");
}

// append elements left in array
for (int i = low; i <= high; i++) {
temporary.append(data[i] + " ");
}

return String.format("%s%n", temporary);
}

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
SecureRandom generator = new SecureRandom();

// create array of 15 random integers in sorted order
int[] data = generator.ints(15, 10, 91).sorted().toArray();
System.out.printf("%s%n%n", Arrays.toString(data)); // display array

// get input from user
System.out.print("Please enter an integer value (-1 to quit): ");
int searchInt = input.nextInt();

// repeatedly input an integer; -1 terminates the program
while (searchInt != -1) {
// perform search
int location = binarySearch(data, searchInt);

if (location == -1) { // not found
System.out.printf("%d was not found%n%n", searchInt);
}
else { // found
System.out.printf("%d was found in position %d%n%n",
searchInt, location);
}

// get input from user
System.out.print("Please enter an integer value (-1 to quit): ");
searchInt = input.nextInt();
}
}
}

• Create a Java application that implement "Binary Search Algorithm" presented in chapter 19 (NOT by
using Java's pre-built binarySearch() method from imported Java library object) to search for an integer
in an array of random integers of size 50 (e.g. 50 random integers) in the range 0 to 2000 inclusive. You
should use Java's random number generator method to randomize the numbers in your random integer
array.
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: binary search for a value in an array require the array to
be sorted in advance, before searching for any value in the array.
Transcribed Image Text:• Create a Java application that implement "Binary Search Algorithm" presented in chapter 19 (NOT by using Java's pre-built binarySearch() method from imported Java library object) to search for an integer in an array of random integers of size 50 (e.g. 50 random integers) in the range 0 to 2000 inclusive. You should use Java's random number generator method to randomize the numbers in your random integer array. 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: binary search for a value in an array require the array to be sorted in advance, before searching for any value in the array.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 3 images

Blurred answer
Similar questions
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY