Write an algorithm that searches a sorted list of \(n\) items by dividing it into three sublists of almost \(n / 3\) items. This algorithm finds the sublist that might contain the given item. and divides it into three smaller sublists of almost equal size. The algorithm repeats this process until it finds the item or concludes that the item is not in the list. Analyze your algorithm, and give the results using order notation.

Short Answer

Expert verified
The ternary search algorithm searches an element in a sorted array by repeatedly dividing the search interval into three parts. It can be implemented using a loop that continues until the searched item is found or when it is determined that it is not present in the list. Its time complexity is \(O(log_3 n)\), as we divide the list into three parts in each step.

Step by step solution

01

Initialization of Boundaries

Start by initializing the boundaries of the search in the list. In the beginning, set the start boundary as the first index (0) and the end boundary as the last index (\(n-1\)) of the list. The searched item will be within these boundaries.
02

Main Loop of the Algorithm

Start the main loop of the algorithm which will continue until the start boundary is less than or equal to the end boundary. This loop will check whether the item is in the list or not. If the start boundary becomes greater than the end boundary, it means the item is not in the list.
03

Establish Midpoints

Within the loop, calculate the first and second midpoints to divide the list into three sublists. The first midpoint will be at \(start + (end-start)/3\) and the second midpoint will be at \(end - (end-start)/3\).
04

Check for the Searched Item

Check if the searched item is at any of the midpoints. If it is, return that index.
05

Update Boundaries

If the searched item is not at the midpoints, it might be in one of the sublists. If the item is less than the value at the first midpoint, update the end boundary to be one step before the first midpoint. If the item is greater than the value at the second midpoint, update the start boundary to be one step after the second midpoint. If the item is between the two midpoints, update the start boundary to be one after the first midpoint, and the end boundary to be one before the second midpoint.
06

Analysis of Time Complexity

The time complexity of a ternary search algorithm is \(O(log_3 n)\). This is because we divide the list into three parts in each step, so the size of the problem reduces to a third in each step.

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

Suppose that, in a divide-and-conquer algorithm, we always divide an in stance of size \(n\) of a problem into 10 subinstances of size \(n / 3,\) and the dividing and combining steps take a time in \(\Theta\left(n^{2}\right)\). Write a recumence equation for the running time \(T(n),\) and solve the equation for \(T(n)\).

When a divide-and-conquer algorithm divides an instance of size \(n\) of a problem into subinstances each of size \(n / c\), the recurrence relation is typically given by \\[ \begin{array}{l} T(n)=a T\left(\frac{n}{c}\right)+g(n) \quad \text { for } n>1 \\ T(1)=d \end{array} \\] where \(g(n)\) is the cost of the dividing and combining processes, and \(d\) is a constant. Let \(n=c^{k}\) (a) Show that \\[ T\left(c^{k}\right)=d \times d^{k}+\sum_{j=1}^{k}\left[a^{k-j} \times g\left(c^{j}\right)\right] \\] (b) Solve the recurrence relation given that \(g(n) \in \Theta(n)\)

Use Mergesort (Algorithms 2.2 and 2.4 ) to sort the following list. Show the actions step by step. \\[ 123 \quad 34 \quad 189 \quad 56 \quad 150 \quad 12 \quad 9 \quad 240 \\]

How many multiplications would be performed in finding the product of two \(64 \times 64\) matrices using Strassen's Method (Algorithm 2.8)?

Write a divide-and-conquer algorithm for the Towers of Hanoi Problem. The Towers of Hanoi Problem consists of three pegs and \(n\) disks of different sizes. The object is to move the disks that are stacked, in decreasing order of their size, on one of the three pegs to a new peg using the third one as a temporary peg. The problem should be solved according to the following rules: (1) when a disk is moved, it must be placed on one of the three pegs: (2) only one disk may be moved at a time, and it must be the top disk on one of the pegs; and (3) a larger disk may never be placed on top of a smaller disk. (a) Show for your algorithm that \(S(n)=2^{n}-1 .\) [Here \(S(n)\) denotes the number of steps (moves), given an input of \(n\) disks. (b) Prove that any other algorithm takes at least as many moves as given in part (a).

See all solutions

Recommended explanations on Computer Science 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