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}.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 16PE: The implementation of a queue in an array, as given in this chapter, uses the variable count to...
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
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning