Question
Create a binary search method called binarySearchAlgorithm(). The element to be searched for will be entered by the user. The binarySearchAlgorithm() method should accept the
Create a binary search method called binarySearchAlgorithm(). The element to be searched for will be entered by the user.
The binarySearchAlgorithm() method should accept the element being searched for as well as the array of elements as parameters. If the element is found, the binarySearchAlgorithm() method will return the index of where the element resides in the array. If the element is not found, the method will return a -1.
Use this main() method:
public static void main(String[] args) { int [] orderedArray = {8, 10, 12, 15, 19, 22, 28, 31, 34, 42, 47, 54, 60, 66, 72, 78, 80, 81, 94, 98, 100}; int element, index;
Scanner keyboard = new Scanner(System.in); System.out.print("Enter an element to find in the ordered array: "); element = keyboard.nextInt();
index = binarySearchAlgorithm(element, orderedArray);
if (index < 0) System.out.println(" Element " + element + " does not exist in this array"); else System.out.println(" Elemnt " + element + " resides at index " + index); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started