package chapter7; import java.util.Scanner; /** This program demonstrates the binary search method in the ArrayTools class. */ public class BinarySearchDemo { public static void main(String[] args) { // The values in the following array are sorted // in ascending order. int numbers[] = {101, 142, 147, 189, 199, 207, 222, 234, 289, 296, 310, 319, 388, 394, 417, 429, 447, 521, 536, 600}; int result, searchValue; String input; // Create the console input objects. Scanner keyboard = new Scanner(System.in); do { // Get a value to search for. System.out.print("Enter a value to search for: "); searchValue = keyboard.nextInt(); // Search for the value result = binarySearch(numbers, searchValue); // Display the results. if (result == -1) System.out.println(searchValue + " was not found."); else { System.out.println(searchValue + " was found at " + "element " + result); } // Consume the remaining newline. keyboard.nextLine(); // Does the user want to search again? System.out.print("Do you want to search again? (Y or N): "); input = keyboard.nextLine(); } while (input.charAt(0) == 'y' || input.charAt(0) == 'Y'); } /** The binarySearch method performs a binary search on an integer array. The array is searched for the number passed to value. If the number is found, its array subscript is returned. Otherwise, -1 is returned indicating the value was not found in the array. @param array The array to search. @param value The value to search for. */ public static int binarySearch(int[] array, int value) { int first; // First array element int last; // Last array element int middle; // Midpoint of search int position; // Position of search value boolean found; // Flag // Set the inital values. first = 0; last = array.length - 1; position = -1; found = false; // Search for the value. while (!found && first <= last) { // Calculate midpoint middle = (first + last) / 2; for (int i=first; i<=last; i++) System.out.print(array[i] + " "); System.out.println(); System.out.println("First = " + first); System.out.println("Last = " + last); System.out.println("Middle = " + middle); // If value is found at midpoint... if (array[middle] == value) { found = true; position = middle; } // else if value is in lower half... else if (array[middle] > value) last = middle - 1; // else if value is in upper half.... else first = middle + 1; } // Return the position of the item, or -1 // if it was not found. return position; } } Modify the binary search algorithm. If there are multiple values in the searched array that matches the desired value, the search method should return an array containing all indexes where the value is located at.   For example, if we are searching 4 in the array {1,2,4,4,5,7}, the method should return {2,3}.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

Enhanced binary search

 BinarySearchDemo.java 

package chapter7; import java.util.Scanner; /** This program demonstrates the binary search method in the ArrayTools class. */ public class BinarySearchDemo { public static void main(String[] args) { // The values in the following array are sorted // in ascending order. int numbers[] = {101, 142, 147, 189, 199, 207, 222, 234, 289, 296, 310, 319, 388, 394, 417, 429, 447, 521, 536, 600}; int result, searchValue; String input; // Create the console input objects. Scanner keyboard = new Scanner(System.in); do { // Get a value to search for. System.out.print("Enter a value to search for: "); searchValue = keyboard.nextInt(); // Search for the value result = binarySearch(numbers, searchValue); // Display the results. if (result == -1) System.out.println(searchValue + " was not found."); else { System.out.println(searchValue + " was found at " + "element " + result); } // Consume the remaining newline. keyboard.nextLine(); // Does the user want to search again? System.out.print("Do you want to search again? (Y or N): "); input = keyboard.nextLine(); } while (input.charAt(0) == 'y' || input.charAt(0) == 'Y'); } /** The binarySearch method performs a binary search on an integer array. The array is searched for the number passed to value. If the number is found, its array subscript is returned. Otherwise, -1 is returned indicating the value was not found in the array. @param array The array to search. @param value The value to search for. */ public static int binarySearch(int[] array, int value) { int first; // First array element int last; // Last array element int middle; // Midpoint of search int position; // Position of search value boolean found; // Flag // Set the inital values. first = 0; last = array.length - 1; position = -1; found = false; // Search for the value. while (!found && first <= last) { // Calculate midpoint middle = (first + last) / 2; for (int i=first; i<=last; i++) System.out.print(array[i] + " "); System.out.println(); System.out.println("First = " + first); System.out.println("Last = " + last); System.out.println("Middle = " + middle); // If value is found at midpoint... if (array[middle] == value) { found = true; position = middle; } // else if value is in lower half... else if (array[middle] > value) last = middle - 1; // else if value is in upper half.... else first = middle + 1; } // Return the position of the item, or -1 // if it was not found. return position; } }

Modify the binary search algorithm. If there are multiple values in the searched array that matches the desired value, the search method should return an array containing all indexes where the value is located at.

 

For example, if we are searching 4 in the array {1,2,4,4,5,7}, the method should return {2,3}.

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 3 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education