Question

JAVA question: Suppose we are performing a binary search on a sorted array called numbers initialized...

JAVA question: Suppose we are performing a binary search on a sorted array called numbers initialized as follows:

// index           0   1   2   3   4   5   6   7   8   9  10  11  12  13
int[] numbers = {-30, -9, -6, -4, -2, -1,  0,  2,  4, 10, 12, 17, 22, 30};
// search for the value -5
int index = binarySearch(numbers, -5);

Write the indexes of the elements that would be examined by the binary search (the mid values in our algorithm's code) and write the value that would be returned from the search. Assume that we are using the binary search algorithm shown in chapter 13 of the Building Java Programs 4th Edition textbook.

Indexes examined:

6, 2, 4, 3

value returned:

-1

This is the answer, but can someone please explain why all of those indexes are examined? Why not just indexes 2 and 3?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

First it will find the mid element as

left =0 and right =13 so 0+13-1/2= 6 so 6 is examined

now numbers[6]>num

so now we will go left and mid-1

left=0 and right=5

mid= 0-5-1/2=2

so arr[2]<-5 so

so now we will go mid+1 and right-1

so left=3 and right 4

so 3+4+1/2=4

so similar to others also

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

Add a comment
Know the answer?
Add Answer to:
JAVA question: Suppose we are performing a binary search on a sorted array called numbers initialized...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Java application that implements the recursive Binary Search alogrithm below: Write a Java application...

    Write a Java application that implements the recursive Binary Search alogrithm below: Write a Java application that implements the recursive Binary Search alogrithm below:/** * Performs the standard binary search using two comparisons * per level. This is a driver that calls the recursive method * @return index where item is found or NOT FOUND if not found */public static <AnyType extends Comparable<? super AnyType>> int binarySearch(AnyType [] a, AnyType x) {return binarySearch(a, x, 0, a.length -1);}/** * Hidden recursive...

  • Java The following questions ask about tracing a binary search. To trace the binary search, list...

    Java The following questions ask about tracing a binary search. To trace the binary search, list the value of first, last, and mid for each pass through the loop or method. Use one of these methods for your trace. public static int binarySearchIterative(int[] numbers, int target) { boolean found = false; int first = 0; int last = numbers.length - 1; while (first <= last && !found) { int mid = (first + last) / 2; if (numbers[mid] == target)...

  • What indexes will be examined as the middle element by a binary search for the target...

    What indexes will be examined as the middle element by a binary search for the target value 8 when the search is run on the following input array? Check if the input array is in sorted order. What can you say about the binary search algorithm’s result? int[] numbers = {6, 5, 8, 19, 7, 35, 22, 11, 9}; int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9}; (Using Java code please)

  • #2Trace These JAVA searches by hand on the following dataset. Sorted Data Set: 3, 5, 8,...

    #2Trace These JAVA searches by hand on the following dataset. Sorted Data Set: 3, 5, 8, 9, 12, 14, 18, 19, 21, 23, 24, 26 #Trace the sorted data set using a binary search. Search target: 23 #Trace the sorted data set using a binary search. Search target: 15 (To trace a binary search, list the value of first, last, and mid for each pass through the method.) Use this method: private static <T extends Comparable<? super T>>    boolean...

  • What indexes will be examined as the middle element by a binary search for the target...

    What indexes will be examined as the middle element by a binary search for the target value 8 when the search is run on the following input array? Check if the input array is in sorted order. What can you say about the binary search algorithm’s result? int[] numbers = {6, 5, 8, 19, 7, 35, 22, 11, 9}; int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9};

  • Given the contents of a sorted array of size 6, shown below, show which indices of...

    Given the contents of a sorted array of size 6, shown below, show which indices of the array will be visited, and in what order, if the algorithm BinarySearch was to be performed on this array for the number 4. int theArray[] = {1, 2, 3, 6, 8, 9}; Following are example question & answer: Given the contents of a sorted array of size 7, shown below, show which indices of the array will be visited, and in what order,...

  • The binary search algorithm is used to search the following array for the value 59. In...

    The binary search algorithm is used to search the following array for the value 59. In the table below, list the values for the subscript variables low, high, and mid for each pass through the algorithm's while loop. (There may be fewer than five passes.) 10 19 24 37 42 48 52 59 65 78 Pass 1: low =  high =  mid = Pass 2: low =  high =  mid = Pass 3: low =  high =  mid = Pass 4: low =  high =  mid = Pass...

  • Unit 4 Assignment 5: Coding Project using C# Binary Search Scenario Use the same integer array...

    Unit 4 Assignment 5: Coding Project using C# Binary Search Scenario Use the same integer array named partNumbers that you used for task 3. Sort the array in ascending order. 1065, 1095, 1075, 1055, 1056, 1090, 1098, 1088, 1097, and 1078. Java: use Array.sort() C#: use Array.Sort() PHP: use sort() Write code that asks the user to enter two part numbers. For C#, use console input. Implement a binary tree search function called binarySearch() to search the array for the...

  • Given the binary search function, answer following question: int binarySearch(int a[], int size, int target, int...

    Given the binary search function, answer following question: int binarySearch(int a[], int size, int target, int low, int high) { while (low <= high) { int mid = (low + high) / 2; if (a[mid] == target) return mid; else if (a[mid] < target) low = mid + 1; else high = mid 1: } return -1; } 1) If array a[] = {4, 5, 18, 25, 66, 70, 78}, size = 7, target = 71, low = 0, high...

  • Given the following code for a binary search, how many times this method have to be...

    Given the following code for a binary search, how many times this method have to be executed in order to find the number 5? A) 1 time B) 2 times C) 3 times D) 4 times E) more than 5 times public class BinarySearch{ public static void main(String []args){ if (right >= left) { int middle = left + (right - left) / 2; if (arr[middle] == num) return middle; if (arr[mid] > num) return binarySearch(arr, left, middle - 1,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT