Question

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) {
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 sorted dataset below. List first, last, and mid for each pass through the loop or each recursive method call.
target = 15

index: 0 1 2 3 4 5 6 7 8 9 10 11
value: 12 15 19 23 25 27 36 39 47 58 62 67

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

index: 0 1 2 3 4 5 6 7 8 9 10 11
value: 12 15 19 23 25 27 36 39 47 58 62 67

0 0
Add a comment Improve this question Transcribed image text
Know the answer?
Add Answer to:
Java The following questions ask about tracing a binary search. To trace the binary search, list...
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
  • 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;...

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

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

  • Complete the binary search function. def binary Search (array, first, last, key): if last >= first:...

    Complete the binary search function. def binary Search (array, first, last, key): if last >= first: mid = int(first + (last - first)/2) if array[mid] return mid key: elif array[mid] > key: ##Statement 1 else: ##Statement 2 else: return -1 Statement 1: return binarySearch(array, last, mid+1, key) Statement 2: return binarySearch(array, mid-1, first, key) Statement 1: return binarySearch(array, first, mid+1, key) Statement 2: return binarySearch(array, mid-1, last, key) Statement 1: return binarySearch(array, mid-1, first, key) Statement 2: return binary Search(array,...

  • C++ Time the sequential search and the binary search methods several times each for randomly generated...

    C++ Time the sequential search and the binary search methods several times each for randomly generated values, then record the results in a table. Do not time individual searches, but groups of them. For example, time 100 searches together or 1,000 searches together. Compare the running times of these two search methods that are obtained during the experiment. Regarding the efficiency of both search methods, what conclusion can be reached from this experiment? Both the table and your conclusions should...

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

  • 2. (10 pts) Recall Binary Search: Input: a list of n sorted integer values and a...

    2. (10 pts) Recall Binary Search: Input: a list of n sorted integer values and a target value Output: True if target value exists in list and location of target value, false otherwise Method: Set left to 1 and right to n Set found to false Set targetindex to - 1 While found is false and left is less than or equal to right Set mid to midpoint between left and right If target = item at mid then set...

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

  • plz solve Modify binary search so that it always returns the element with the smallest index...

    plz solve Modify binary search so that it always returns the element with the smallest index * that matches the search element (and still guarantees logarithmic running time). import java.util.Arrays; public class BinarySearch2 { public static int rank(int key, int[] a) { // Array must be sorted. int lo = 0; int hi = a.length - 1; while (lo <= hi) { // Key is in a[lo..hi] or not present. int mid = lo + (hi - lo) / 2;...

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