Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

How many comparisons are needed for a binary search in a set of 64 elements?

Short Answer

Expert verified

The number of comparisons needed is 14.

Step by step solution

Achieve better grades quicker with Premium

  • Unlimited AI interaction
  • Study offline
  • Say goodbye to ads
  • Export flashcards

Over 22 million students worldwide already upgrade their learning with Vaia!

01

Recurrence Relation definition

A recurrence relation is an equation that recursively defines a sequence where the next term is a function of the previous terms:f(n) = a f(n / b) + c

02

Apply the Recurrence Relation

Binary search divides each list into two sub-lists and determines in which sub-list the elements will need to be present (since the initial list is sorted).

Letf(n) represent the number of comparisons in a set of nelements.

The binary search thus makes 2 comparisons in each step (to the two sub-lists), while the number of elements in the new list is half the number of elements of the original list:

f(n) = f(n / 2) + 2

Repeatedly apply the previous relationship with n=64:

f(64)=f(32)+2f(64)=f(16)+4f(64)=f(8)+6f(64)=f(4)+8f(64)=f(2)+10f(64)=f(1)+12f(64)=2+12f(64)=14

When the list contains 1 the element, then we require 2 comparisons.

Thus we require 14 comparisons

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

In this exercise we construct a dynamic programming algorithm for solving the problem of finding a subset S of items chosen from a set of n items where item i has a weight , which is a positive integer, so that the total weight of the items in S is a maximum but does no exceed a fixed weight limit W. Let M(j,w)denote the maximum total weight of the items in a subset of the first j items such that this total weight does not exceed w. This problem is known as the knapsack problem.

a) Show that ifwj>w, thenM(j,w)=M(j-1,w).
b) Show that if wjโ‰คw, thenM(j,w)=max(M(j-1,w),wj+Mj-1,w-wj).
c) Use (a) and (b) to construct a dynamic programming algorithm for determining the maximum total weight of items so that this total weight does not exceed W. In your algorithm store the valuesM(j,w) as they are found.
d) Explain how you can use the values M(j,w)computed by the algorithm in part (c) to find a subset of items with maximum total weight not exceeding W.

Use Exercise 29 to show that if a=bd, then f(n)is O(ndlogn).

How many ternary strings of length six do not contain two consecutive0'sor twoconsecutive1's?

Find a closed form for the generating function for each of these sequences. (Assume a general form for the terms of the sequence, using the most obvious choice of such a sequence.)

a) \( - 1, - 1, - 1, - 1, - 1, - 1, - 1,0,0,0,0,0,0, \ldots \)

b) \(1,3,9,27,81,243,729, \ldots \)

c) \(0,0,3, - 3,3, - 3,3, - 3, \ldots \)

d) \(1,2,1,1,1,1,1,1,1, \ldots \)

e) \(\left( {\begin{array}{*{20}{l}}7\\0\end{array}} \right),2\left( {\begin{array}{*{20}{l}}7\\1\end{array}} \right),{2^2}\left( {\begin{array}{*{20}{l}}7\\2\end{array}} \right), \ldots ,{2^7}\left( {\begin{array}{*{20}{l}}7\\7\end{array}} \right),0,0,0,0, \ldots \)

f) \( - 3,3, - 3,3, - 3,3, \ldots \)

g) \(0,1, - 2,4, - 8,16, - 32,64, \ldots \)

h) \(1,0,1,0,1,0,1,0, \ldots \)

LetG(x)be the sequence of Catalan numbers, that is, the solution to the recurrence relationwith.

(a)Show that ifis the generating function for the sequence of Catalan numbers, thenxG(x)2-G(x)+1=0. Conclude (using the initial conditions) thatG(x)=(1-1-4x)/(2x).

(b) Use Exercise 40 to conclude that G(x)=โˆ‘n=0โˆž1n+1(2nn)xn,so thatCn=1n+1(2nn).

(c) Show thatCnโฉพ2n-1for all positive integersn.

See all solutions

Recommended explanations on Math Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free