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

Describe the worst-case time complexity, measured in terms of comparisons, of the ternary search algorithm described in Exercise 27 of Section 3.1.

Short Answer

Expert verified

O(log n)

Step by step solution

01

Write algorithm

Result previous exercise:

Procedure ternary (a1,a2,...an:integerswithn1,x:integer)

i:=1j:=n

While i < j - 1

I:=(i+j)/3u:=2(i+j)/3

if xauthen data-custom-editor="chemistry" i:=u+1else

if xaithen

i=I+1j=u

else j := l

if x=ai then location:=ielse

if x=aithen location:=j else

location:=0

return location.

02

Prove

The algorithm makes comparison in each iteration of the while-loop (i<j-1)and makes at most 2 comparisons in the if-loop role="math" localid="1668622461697" (xauandxalandxauisfalse). Thus, in every iteration of the while-loop at most 1 + 2 = 3 comparisons are made.

The number of iterations is the number of times we divide the interval into 3 pieces, which is thus the power x of 3 such that 3xnand thus x=log3n.

The number of comparisons is then the product of the number of iterations and the number of comparisons per iteration:

Number of comparisons=log3n3=3log3n

Thus, at most 3log3ncomparisons are made and 3log3n is O(logn).

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

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

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

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