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

for an array A of size N, and A[0] != A[n-1]. devise an efficient algorithm for...
for an array A of size N, and A[0] != A[n-1]. devise an efficient algorithm for find a pair of elements A[i] and A[i+1] such that A[i] != A[i+1]. can you always find such pair and why
Convolve the following two signals using the INPUT side algorithm. x[n]= 1, 0, 2, 0, 0,...
Convolve the following two signals using the INPUT side algorithm. x[n]= 1, 0, 2, 0, 0, 0, 2, 1, 0, 1 h[n]= 3, 2, 1 y[n]=?
Consider the following pseudocode for insertion-sort algorithm. The algorithm sorts an arbitrary array A[0..n − 1]...
Consider the following pseudocode for insertion-sort algorithm. The algorithm sorts an arbitrary array A[0..n − 1] of n elements. void ISORT (dtype A[ ], int n) { int i, j; for i = 1 to n – 1 {     //Insert A[i] into the sorted part A[0..i – 1]     j = i;     while (j > 0 and A[j] < A[j – 1]) {         SWAP (A[j], A[j – 1]);         j = j – 1 }     }...
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...
We are given an array of n numbers A in an arbitrary order. Design an algorithm...
We are given an array of n numbers A in an arbitrary order. Design an algorithm to find the largest and second largest number in A using at most 3/2n -2 comparisons. (i) describe the idea behind your algorithm in English (3 points); (ii) provide pseudocode (5 points); (iii) analyze the number of comparisons used in your algorithm (2 points).
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT