Question

In: Computer Science

algorithm binarySearch input bottom, top: a number    a: array a[0] to a[n-1] of numbers    x: a...

algorithm binarySearch

input

bottom, top: a number

   a: array a[0] to a[n-1] of numbers

   x: a number

output

result:

true if x in a

false otherwise

Sideeffect NA

Plan

if (top < bottom) result := false

else

// get the middle of the array (refining)

middle := (top - bottom)/2 + bottom (refining by +1 or -1 or integer division …)

// example what is midpoint between 6 and 8

// (8-6)/2 = 1 we need to add 6 to get the “absolute middle point”

if (x is a[middle])

result := true

else

if x is larger than middle element

// find if x is in the second half

result := binarySearch(middle+1, top,a,x)

Else

// find if x is in the first half

result := binarySearch(bottom, middle-1, a, x)

Following the binary search algorithm (final version) in the lecture notes, write a recurrence relation for the running time T(n), where n is the input size, of the algorithm. 2) Give the best asymptotic upper bound for the running time of the algorithm.

Solutions

Expert Solution

bottom, top: a number

   a: array a[0] to a[n-1] of numbers

   x: a number

output

result:

true if x in a

false otherwise

Sideeffect NA

Plan

if (top < bottom) result := false

else

// get the middle of the array (refining)

middle := (top - bottom)/2 + bottom (refining by +1 or -1 or integer division …) //O(1)

// example what is midpoint between 6 and 8

// (8-6)/2 = 1 we need to add 6 to get the “absolute middle point”

if (x is a[middle]) //O(1)

result := true

else

if x is larger than middle element

// find if x is in the second half

result := binarySearch(middle+1, top,a,x) //T(n/2)

Else

// find if x is in the first half

result := binarySearch(bottom, middle-1, a, x) //T(n/2)

1. T(n)= { c if(i>=j) }

{ T(n/2)+c if(i<j) }

Derivation->

T(n)=T(n/2) + 1

T(n/2)=T(n/4) + 1 ……[ T(n/4)= T(n/2^2) ]

T(n/4)=T(n/8) + 1 ……[ T(n/8)= T(n/2^3) ]

.**

**

**

.kth step=> T(n/2^k-1)=T(n/2^k) + 1*(k times)

Adding all the equations we get, T(n) = T(n/2^k) + k times 1 _____eq(final)

n/2^k= 1

n=2^k

log n=k [taken log(base 2) on both sides ]

Put k= log n in eq(final)

T(n) = T(1) + log n

T(n) = 1 + log n [we know that T(1) = 1 , because it’s a base condition as we are left with only one element in the array and that is the element to be searched so we return 1]

T(n) = O(log n) [taking dominant polynomial, which is n here)

2. Time Complexity:

  • Best Case: O(1)
  • Average Case: O(logn)
  • Worst Case: T(n)=T(n/2)+c, By using master Theorem O(logn)

This is how we got “log n” time complexity for binary search. Hope this might help :) .


Related Solutions

Consider the following program specification: Input: An integer n > 0 and an array A[0..(n –...
Consider the following program specification: Input: An integer n > 0 and an array A[0..(n – 1)] of n integers. Output: The largest index b such that A[b] is the largest value in A[0..(n – 1)]. For example, if n = 10 and A = [ 4, 8, 1, 3, 8, 5, 4, 7, 1, 2 ] (so A[0] = 4, A[1] = 8, etc.), then the program would return 4, since the largest value in A is 8 and...
. We have the following algorithm. Algorithm Novel(X[0..m-1, 0..m-1]) //Input: A matrix X[0..m-1, 0..m-1] of real...
. We have the following algorithm. Algorithm Novel(X[0..m-1, 0..m-1]) //Input: A matrix X[0..m-1, 0..m-1] of real numbers for i←0 to m-2 do for j←i+1 to m-1 do    if X[i,j] ≠X[j, i]        return false return true a.   What does this algorithm compute? b. What is its basic operation? c. How many times is the basic operation executed? d. What is the efficiency class of this algorithm?
Algorithm1 prefixAverages1(X, n) Input array X of n integers Output array A of prefix averages of...
Algorithm1 prefixAverages1(X, n) Input array X of n integers Output array A of prefix averages of X A ← new array of n integers for i ← 0 to n − 1 do s ← X[0] for j ← 1 to i do s ← s + X[j] A[i] ← s / (i + 1) return A ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Algorithm2 prefixAverages2(X, n) Input array X of n integers Output array A of prefix averages of X A ← new array of...
Consider the following algorithm. Algorithm Mystery(n) //Input: A nonnegative integer n S ← 0 for i...
Consider the following algorithm. Algorithm Mystery(n) //Input: A nonnegative integer n S ← 0 for i ← 1 to n do S ← S + i * i return S a. What does this algorithm compute? b. What is its basic operation? c. How many times is the basic operation executed? d. What is the efficiency class of this algorithm? e. Suggest an improvement, or a better algorithm altogether, and indicate its efficiency class. If you cannot do it, try...
give an algorithm where given as input an array A[1...n] withthe following property. There exists...
give an algorithm where given as input an array A[1...n] with the following property. There exists an index I such that if we append A[1...i−1] after A[i...n], we get an array in sorted increasing order. For simplicity you can assume that n is a power of 2.Give an efficient algorithm that returns the smallest element in A. Analyze the time complexity of your algorithm.Hint: you may want to compare A[1] and A[n/2].
Write a program to implement the Romberg Algorithm. Input: f(x), an interval [a,b], N (the number...
Write a program to implement the Romberg Algorithm. Input: f(x), an interval [a,b], N (the number of subintervals on the finest grid on level 0 is 2^N, therefore, N is usualy a small integer) Output: the triangle generated by Romberg Algorithm.
In Liinear-time selection algorithm where the input array of numbers are cut into groups of size...
In Liinear-time selection algorithm where the input array of numbers are cut into groups of size 5. Show that, when the group size is 7, the algorithm still runs in linear time.
Given an array A[1..n] of integers - all of whose numbers are in the range [0,...
Given an array A[1..n] of integers - all of whose numbers are in the range [0, n^3 − 1] give an algorithm which sorts them in O(n) time.
Suppose that the array X consists of real numbers X[1], X[2], …, X[N]. Write a pseudocode...
Suppose that the array X consists of real numbers X[1], X[2], …, X[N]. Write a pseudocode program to compute the minimum of these numbers.
Let A be an array of non-decreasing N integers. Write an algorithm that returns the number...
Let A be an array of non-decreasing N integers. Write an algorithm that returns the number of pairs of integers in A that are equal. Analyze your algorithm thoroughly. Your analysis should include a thorough examination of both the best and the worst-case scenarios. This includes a description of what the best and worst cases would look like. You should include both space and time in your analysis, but keep in mind that space refers to “extra space,” meaning in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT