Question

Do a trace on the code below. In the method binarySearch below: variable key holds the...

Do a trace on the code below. In the method binarySearch below: variable key holds the value 27, and

variable list is a reference to an array with these values {12, 25, 36, 39, 43, 65, 78, 86, 99, 108, 121}.

public static int binarySearch(int[] list, int key) {

    int lowIndex = 0;

    int highIndex = list.length - 1;

    while (highIndex >= lowIndex) {

      int midIndex = (lowIndex + highIndex) / 2;

      if (key < list[midIndex]){

           highIndex = midIndex - 1;

     }

      else if (key > list[midIndex]){

           lowIndex = midIndex + 1;      

     }

      else if (key == list[midIndex]){

           return midIndex;            

     }

    } // end of while loop

    return -1;

} // end of binary search method

Each row in the table below corresponds to one iteration of the while loop in the method above. You can add or remove rows according to the actual number of iterations. The first row’s information corresponds to the first iteration, and its value has been filled. You need to continue for the following rows.

key

lowIndex

highIndex

highIndex>=lowIndex

midIndex

key==list[midIndex]

key<list[midIndex]

27

0

10

true

5

false

true

Given the key value and array content listed above, what is the return value of the binary search method?

Re-do the code tracing, and this time, the key is changed to 80, as indicated by the table below.

key

lowIndex

highIndex

highIndex>=lowIndex

midIndex

key==list[midIndex]

key<list[midIndex]

80

0

10

true

5

false

false

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

Given list is {12, 25, 36, 39, 43, 65, 78, 86, 99, 108, 121}

key

lowIndex

highIndex

highIndex>=lowIndex

midIndex

key==list[midIndex]

key<list[midIndex]

27

0

10

true

5

false

true

27 0 4 true 2 false true
27 0 1 true 0 false false
27 1 1 true 1 false false
27 2 1 false

So. key 27 not found in the array

Given list is {12, 25, 36, 39, 43, 65, 78, 86, 99, 108, 121}

key

lowIndex

highIndex

highIndex>=lowIndex

midIndex

key==list[midIndex]

key<list[midIndex]

80

0

10

true

5

false

false

80 6 10 true 8 false false
80 6 7 true 6 false true
80 7 7 true 7 false true
80 8 7 false

So. key 80 also not found in the array

Add a comment
Know the answer?
Add Answer to:
Do a trace on the code below. In the method binarySearch below: variable key holds the...
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
  • Need help with Python (BinarySearch), code will be below after my statements. Thank you. Have to...

    Need help with Python (BinarySearch), code will be below after my statements. Thank you. Have to "Add a counter to report how many searches have been done for each item searched for." Have to follow this: 1) you'll create a counter variable within the function definition, say after "the top = len(myList)-1" line and initialize it to zero. 2) Then within the while loop, say after the "middle = (bottom+top)//2" line, you'll start counting with "counter += 1" and 3)...

  • 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)...

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

  • Searching/sorting tasks and efficiency analysis - Binary Search Search for the character S using the binary...

    Searching/sorting tasks and efficiency analysis - Binary Search Search for the character S using the binary search algorithm on the following array of characters: A E G K M O R S Z. For each iteration of binary search use a table similar to the table below to list: (a) the left index and (b) the right index of the array that denote the region of the array that is still being searched, (c) the middle point of the array,...

  • Qi. Create a java project Question that includes: • A bubble sort method to sort your...

    Qi. Create a java project Question that includes: • A bubble sort method to sort your arrays. Implement the below recursive binary search. • A java main class where you: o Create an array of characters that contains the letters of your first name followed by your last name, without spaces. o Call the sorting method to sort the array o Call binary search method to find the position of the first letter 'a' in your array. // Recursive Binary...

  • C++ Need the step count for this function. int binarySearch(const int array[], int size, int value)...

    C++ Need the step count for this function. int binarySearch(const int array[], int size, int value) { int first = 0, last = size − 1, middle, position = −1; bool found = false; while (!found && first <= last) { middle = (first + last) / 2; if (array[middle] == value) { found = true; position = middle; } else if (array[middle] > value) last = middle − 1; else first = middle + 1; } return position; }

  • With the following code answer the following questions. describe what happens when the following code is...

    With the following code answer the following questions. describe what happens when the following code is executed: String[] searchMe = {"apple","bear","cat","dog","elephant"}; describe what is being created when this statement executes System.out.println(linearFind("cat",searchMe)); describe the values passed to the method describe how each of the specific values are compared to each other describe when the method stops executing and/or when the loop stops executing describe what is returned to beoutprinted System.out.println(binaryFind("apple",searchMe)); describe the values passed to the method describe how each of...

  • Java Programming Write a program to find the number of comparison using binarySearch and the sequentialSearch...

    Java Programming Write a program to find the number of comparison using binarySearch and the sequentialSearch algorithms as follows: Suppose list is an array of 2500 elements. 1. Use a random number generator to fill list; 2. Use a sorting algorithm to sort list; 3. Search list for some items as follows: a) Use the binary search algorithm to search list (please work on SearchSortAlgorithms.java and modify the algorithm to count the number of comparisons) b) Use the sequential search...

  • You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement...

    You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement this method return new int[] {}; }    There is a utility method provided for you with the following signature. You may use this method to convert a list of integers into an array. public static int[] convertIntegers(List<Integer> integers) Provided code import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class MatrixSearch { // This method converts a list of integers to an array...

  • (10 pts)3-1. Given the following piece of code #define SIZE 10 include <iostream> using namespace std;...

    (10 pts)3-1. Given the following piece of code #define SIZE 10 include <iostream> using namespace std; // sorted array in descending order int list[SIZE] (23, 19, 17, 13, 11, 7, 5, 3, 1, 0); // recursively binary search the array list for key // return the index to list if key is found. else return -1 int recBinarySearch (int key) // Please implement the recursive function.. Please implement the C++ function recBinarySearch that recursively binary searches the value key in...

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