Question

1. [3] Is the following version of binary search algorithm correct? Algorithm 1 Version of Binary search function BSEARCH(A,
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The above-given algorithm is not correct. and the counter-example for the same is option 3

A = [1 ,3,6,7,12,15,32,47]
1st Iteration: first =1, last = 8, mid = 4 => a[mid] < key => first = mid+1, last = last
2nd Iteration first = 5, last = 8, mid = 6, => a[mid] < key => first = mid+1, last = last
3rd Iteration first = 7, last = 8, mid = 7 => a[mid] > key =>first = first, last = mid
4th Iteration first = 7, last = 7, mid = 7 => a[mid] > key => first = frist, last = mid
5th Iteration first = 7, last = 7, mid = 7

Thus 4th and 5th iterations are both same thus it will not make any difference, thus we won't be able to reach to our solution.

That was a nice question to answer
Friend, If you have any doubts in understanding do let me know in the comment section. I will be happy to help you further.
Thanks

Add a comment
Know the answer?
Add Answer to:
1. [3] Is the following version of binary search algorithm correct? Algorithm 1 Version of Binary...
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
  • 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,...

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

  • Show the steps of the binary search algorithm (pseudocode is given below); low = index of...

    Show the steps of the binary search algorithm (pseudocode is given below); low = index of first item in list high = index of last item in list while low is less than or equal to high mid = index halfway between low and high if item is less than list[mid] high = mid - 1 else if item is greater than list[mid] low = mid + 1 else return mid end while return not found For each step of...

  • You are making a .h file. Implement a recursive binary search function. bSearch passes in an...

    You are making a .h file. Implement a recursive binary search function. bSearch passes in an array of integers, the size of the array, and the value the function is searching for. The function should return the index where found or -1 if not found. You will want to implement a recursive helper function that passes in the array, the value to be located, and the beginning and ending of the range of values to search within. Use the provided...

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

  • Read the following pseudocode, note that A is a sorted array: BINARY-SEARCH(A, v) low := 1...

    Read the following pseudocode, note that A is a sorted array: BINARY-SEARCH(A, v) low := 1 high := n while low <= high mid = floor((low+ high)/2) if v == A[mid] return mid else if v > A[mid] low = mid + 1 else high = mid - 1 return NIL a) Find the Recurrence Relation. b) Is there a tight bound? If the answer is yes, find it. c) Find the best case and worst case running time.

  • Read the following pseudocode, note that A is a sorted array: BINARY-SEARCH(A, v) low := 1...

    Read the following pseudocode, note that A is a sorted array: BINARY-SEARCH(A, v) low := 1 high := n while low <= high mid = floor((low+ high)/2) if v == A[mid] return mid else if v > A[mid] low = mid + 1 else high = mid - 1 return NIL a) Find the Recurrence Relation. b) Is there a tight bound? If the answer is yes, find it. c) Find the best case and worst case running time.

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

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

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

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