Question

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.

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

The given pseudocode is of iterative binary search algorithm, which returns the index of some element that equals the given value(v) in a sorted array (A).
where A: sorted array,
mid: index of middle element of array,
low: 1st element of the array,
hihg: last element of the array,

Now, we first compare the given element (v) with element at middle index of the array. If v matches with middle element i.e. a[mid], then we return middle index(mid). Otherwise, we check the middle element against the key, if it is greater, we check for the first half i.e. left half of array else we check the second half i.e. right half of the array and repeat the same process.

Sol a)
Calculating Recurrence Relation:

  • Let say the iteration in Binary Search terminates after k iterations.
  • At each iteration, the array is divided by half. So let’s say the length of array at any iteration is n
  • At Iteration 1, Length of array = n
  • At Iteration 2, Length of array = n⁄2

  • At Iteration 3, Length of array = (n⁄2)⁄2 = n⁄22

  • Therefore, after Iteration k, Length of array = n⁄2k .

    Also, after k divisions, the length of array becomes 1. Therefore, Length of array = n⁄2k = 1

  • Thus, n = 2k
  • Applying log function on both sides:
    • => log2 (n) = log2 (2k)
    • => log2 (n) = k log2 (2)
    • => k = log2 (n) (Since loga (a) = 1 )

Hence, the time complexity of Binary Search is log2 (n) or simply put O(log(n))

and Recurrence Relation is : T(n) = T(n/2) + O(1) and T(1) = T(0) = O(1)

Sol b)

Yes, there is a tight bound.

The lower bound for searching in a sorted array using only comparisons is o(log n) and the upper bound complexity is O(log n) (as deducted in sol (a)).

Tight bound (big Theta) takes both upper (big O) and lower bounds (big Omega) into account.
Since, the upper bound and lower bound coincide, O(log n)+O(log n)=O(2log n) and for large values of n, the constants are ignored in Big O notation. So, O(2log n) = O(log n)

Hence, the complexity is: O(log n).

Sol c)

Time Complexity:

  • · Best Case: O(1)
  • · Average Case: O(logn)
  • · Worst Case: T(n)=T(n/2)+c, By using master Theorem O(logn)
Add a comment
Know the answer?
Add Answer to:
Read the following pseudocode, note that A is a sorted array: BINARY-SEARCH(A, v) low := 1...
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
  • 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.

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

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

  • Consider the following: Algorithm 1 Smallest (A,q,r) Precondition: A[ q, ... , r] is an array...

    Consider the following: Algorithm 1 Smallest (A,q,r) Precondition: A[ q, ... , r] is an array of integers q ≤ r and q,r ∈ N. Postcondition: Returns the smallest element of A[q, ... , r]. 1: function Smallest (A , q , r) 2: if q = r then 3: return A[q] 4: else 5: mid <--- [q+r/2] 6: return min (Smallest(A, q, mid), Smallest (A, mid + 1, r)) 7: end if 8: end function (a) Write a recurrence...

  • without coding Give the Big O run-time of the following algorithms. Binary Search: def binary-search (arr,...

    without coding Give the Big O run-time of the following algorithms. Binary Search: def binary-search (arr, low, high, x): # Check base case if low > high : return None else: mid = (high + low) // 2 element arr[mid] == X: if element return mid elif element > X: return binary-search(arr, low, mid 1, x) else: return binary_search(arr, mid + 1, high, x) Selection Sort: def selection_sort (arr): for i in range (len(arr)): smallest index = i smallest value...

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

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

  • PLEASE write in C Language, ( you can use array, recursive function, Pointers, Dynamic call by re...

    PLEASE write in C Language, ( you can use array, recursive function, Pointers, Dynamic call by refernce, Structure) a) 70 marks] Write a program that Sorts a given integer array via Selection Sort obtained by rotation around 3. Searches for a key point in the rotated array in O(logn) time, where the rotation Rotates the sorted array around a random point, e.g., 1 2 345->34512 is point is known in advance. ts above by first finding the unknown rotation point...

  • When binary searching a sorted array that contains more than one key equal to the search...

    When binary searching a sorted array that contains more than one key equal to the search key, the client may want to know the index of either the first or the last such key. Accordingly, implement the following API: public class BinarySearchDeluxe { /* Returns the index of the first key in a[] that equals the search key, or -1 if no such key. */ public static int firstIndexOf(Key[] a, Key key, Comparator comparator) /* Returns the index of the...

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