Question

Use one of these methods for your trace. public static int binarySearchIterative(int[] numbers, int target) {...

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) {
         targetLocation = mid;
         found = true;
      } else if (numbers[mid] < target) {
         first = mid + 1;
      } else { // numbers[mid] > target
         last = mid - 1;
      }
   }
   return targetLocation;
}

public static int binarySearchRecursive(int[] numbers, int target) {
   return binarySearchRecursiveHelper(numbers, target, 0, numbers.length - 1);
}

private static int binarySearchRecursiveHelper(int[] numbers, int target, int first, int last) {
   int mid = ((last - first) / 2) + first;

   if (first > last) {
      return -1; // indices cross over
   } else if (target == numbers[mid]) {
      return mid; // we found it!
   } else if (target < numbers[mid]) {
      return binarySearchRecursiveHelper(numbers, target, first, mid - 1);
   } else { // target > numbers[mid]
      return binarySearchRecursiveHelper(numbers, target, mid + 1, last);
   }
}

1. Trace binary search on the dataset below. List first, last, and mid for each pass through the loop or each recursive method call.

[3, 5, 8, 9, 12, 14, 18, 19, 21, 23, 24, 26]

target = 23

2. Trace binary search on the dataset below. List first, last, and mid for each pass through the loop or each recursive method call.

[3, 5, 8, 9, 12, 14, 18, 19, 21, 23, 24, 26]

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

Please find your answers below and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Array: [3, 5, 8, 9, 12, 14, 18, 19, 21, 23, 24, 26]

Target: 23

Pass

First

Last

Mid

1

0

11

5

2

6

11

8

3

9

11

10

4

9

9

9

(element 23 is found at index 9 after 4 iterations / recursive calls)

Array: [3, 5, 8, 9, 12, 14, 18, 19, 21, 23, 24, 26]

Target: 16

Pass

First

Last

Mid

1

0

11

5

2

6

11

8

3

6

7

6

4

6

5

6

(Element 16 is not found, a total of 4 recursive calls are made)

Add a comment
Know the answer?
Add Answer to:
Use one of these methods for your trace. public static int binarySearchIterative(int[] numbers, int target) {...
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
  • 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)...

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

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

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

  • Trace this code show the work Public static int funcQ1(int m, int n) { if (m...

    Trace this code show the work Public static int funcQ1(int m, int n) { if (m < n) return 0; else return 1 + funQ1(m-n, n); } • What is the value of funcQ1(6, 3), based on the code above? • What is the value of funcQ1(9, 7), based on the code above? • What is the value of funcQ1(-5, 1), based on the code above?

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

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

  • The binary search algorithm from Chapter 9 is a very efficient algorithm for searching an ordered...

    The binary search algorithm from Chapter 9 is a very efficient algorithm for searching an ordered list. The algorithm (in pseudocode) is as follows: highIndex - the maximum index of the part of the list being searched lowIndex - the minimum index of the part of the list being searched target -- the item being searched for //look in the middle middleIndex = (highIndex + lowIndex) / 2 if the list element at the middleIndex is the target return the...

  • 1. t(n) is the runtime of following function, public static int f4(int [] a, int start,...

    1. t(n) is the runtime of following function, public static int f4(int [] a, int start, int end){ int ans = 0; if (start >= end) ans = a[start]; else { int mid = (start + end) / 2; int x = f4(a, start, mid); int y = f4(a, mid + 1, end); print(a, start, end); //print each element in a from start to end if (x < y) ans = x; else ans = y; } return ans; }...

  • Java, how would i do this public static void main(String[] args) { int n = 3;...

    Java, how would i do this public static void main(String[] args) { int n = 3; int result; result = factorial(n); + public static int factorial(int n) public static int factorial(int n) n = 3 { returns 3* 2 * 1 { if (n == 1) return n; else return (n * factorial(n-1)); if (n == 1) return n; else return (3 * factorial(3-1)); ܢܟ } public static int factorial(int n) n = 2 public static int factorial(int n) returns...

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