Question
Question 1 [20 marks] A pseudocode of the binary search algorithm is given below. Algorithm BinarySearch(A[0..n 1], K) //Implements nonrecursive binary search //Input: An array
Question 1 [20 marks] A pseudocode of the binary search algorithm is given below. Algorithm BinarySearch(A[0..n 1], K) //Implements nonrecursive binary search //Input: An array A[0..n 1] sorted in ascending order and // a search key K //Output: An index of the arrays element that is equal to K // or 1 if there is no such element l 0; r n 1 while l r do
{ m [ ( l + r ) / 2 ] if K = A[m] return m else if K < A[m] r m 1 else l m + 1
}
return 1 a. [5 marks] Set up a recurrence relation for the number of key comparisons made by the binary search algorithm in the worst case Cw(n). b. [15 marks] Solve the recurrence relation Cw(n) by the backward substitution method for n = 2k
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started