1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Reil [10]
3 years ago
8

The ternary search algorithm locates an element in a list of increasing integers by successively splitting the list into three s

ub-lists of equal (or as close to equal as possible) size, and restricting the search to the appropriate piece. Specify the steps of this algorithm.
Mathematics
1 answer:
Zigmanuir [339]3 years ago
6 0

Answer:

Algorithm

The steps involved in this algorithm are:

(The list must be in sorted order)

• Step 1: Divide the search space (initially, the list) in three parts (with two mid-points: mid1 and mid2)

• Step 2: The target element is compared with the edge elements that is elements at location mid1, mid2 and the end of the search space. If element matches, go to step 3 else predict in which section the target element lies. The search space is reduced to 1/3rd. If the element is not in the list, go to step 4 or to step 1.

• Step 3: Element found. Return index and exit.

• Step 4: Element not found. Exit.

Complexity

• Worst case time complexity: O(log N)

• Average case time complexity: O(log N)

• Best case time complexity: O(1)

• Space complexity: O(1)

Algorithm

The steps involved in this algorithm are:

(The list must be in sorted order)

• Step 1: Divide the search space (initially, the list) in three parts (with two mid-points: mid1 and mid2)

• Step 2: The target element is compared with the edge elements that is elements at location mid1, mid2 and the end of the search space. If element matches, go to step 3 else predict in which section the target element lies. The search space is reduced to 1/3rd. If the element is not in the list, go to step 4 or to step 1.

• Step 3: Element found. Return index and exit.

• Step 4: Element not found. Exit.

Complexity

• Worst case time complexity: O(log N)

• Average case time complexity: O(log N)

• Best case time complexity: O(1)

• Space complexity: O(1)

#include <stdio.h>

int ternarySearch(int array[], int left, int right, int x)

{

  if (right >= left) {

    int intvl = (right - left) / 3;

    int leftmid = left + intvl;

    int rightmid = leftmid + intvl;

    if (array[leftmid] == x)

       return leftmid;

    if (array[rightmid] == x)

       return rightmid;

    if (x < array[leftmid]) {

      return ternarySearch(array, left, leftmid, x);

    }

    else if (x > array[leftmid] && x < array[rightmid]) {

      return ternarySearch(array, leftmid, rightmid, x);

    }

    else {

      return ternarySearch(array, rightmid, right, x);

    }

  }

  return -1;

}

int main(void)

{

  int array[] = {1, 2, 3, 5};

  int size = sizeof(array)/ sizeof(array[0]);

  int find = 3;

  printf("Position of %d is %d\n", find, ternarySearch(array, 0, size-1, find));

  return 0;

}

Step-by-step explanation:

You might be interested in
What are the intercepts of the line?
Ann [662]
D is not the answer bc it is a 


8 0
3 years ago
Read 2 more answers
What is the approximate circumference of the circle shown below?​
aniked [119]

Answer:

D: 109 cm

Step-by-step explanation:

The formula of circumference is

\pi d

Diameter =

17.4 \times 2 = 34.8

Hence, circumference =

34.8 \times \pi = 109.32742...

rounded of to 109.

8 0
3 years ago
Read 2 more answers
A cooler has 12 apple juices and 15 grape juices. 9 of the apple juices are sugar free and 5 of the grape juices are sugar free.
antoniya [11.8K]
To me: given that the juice is sugar free means that I can ignore the 3 sugar apple juices and the 10 sugr grape juices.

Therefore the field of drinks is 14 sugar free drinks and the changes that it is apple juice, is 9:14 or 9/14.

If, on the other hand, the problem is saying that the juice must be sugar free and also apple, that would be 9/27 or 1/3
8 0
3 years ago
Read 2 more answers
Find the area of the following ellipse. a = 4.5 m; b = 5.5 m
LenaWriter [7]

Answer:

Area of ellipse is ≈ 77.78 m^{2}

Step-by-step explanation:

Given the dimension of ellipse

  a = 4.5 m

  b = 5.5 m

Now, Area of ellipse = \pi ab

                                  = \frac{22}{7}\times 4.5\times 5.5

                                  = \frac{5445}{70}

                                  = 77.78 m^{2}

Hence Area of ellipse will be 77.78 m^{2}

6 0
3 years ago
Read 2 more answers
Solve the inequality. Graph the solution set.<br><br> 2r−9≤−6
Ahat [919]
We have that

2r−9≤−6------> 2r ≤ -6+9-------> 2r ≤  3-----> r ≤ 1.5

the solution is the interval  (-∞, 1.5]

using a graph tool
see the attached figure

5 0
3 years ago
Read 2 more answers
Other questions:
  • What's 7523987 to 1 significant figure
    6·2 answers
  • What is the slope of the line that contains the cordon and points (8, -3) and (-2, 7)​
    11·1 answer
  • Use division to determine whether the ratios form a proportion.
    11·1 answer
  • The top of a desk is shaped like a trapezoid. The bases of the trapezoid are 26.5 and 30 centimeters long. The area of the desk
    7·1 answer
  • Sue has 2 cats. Each cat eats of a tin of cat food each day. Sue buys 8 tins of cat food. Has Sue bought enough cat food to feed
    7·1 answer
  • Two BLARBs equal one BLABB. Three BLABBs equal ten BLALBs, and one BLALB equals two BABs. How many BABs equal 6 BLARBs?
    5·1 answer
  • Mr. Thompson needs to move two large boxes of toys from the store to his house. The first box measures 6 ft long × 4 ft tall × 5
    5·1 answer
  • 1-1/2=2(y=4) please help me
    11·1 answer
  • (03.05 MC)
    8·1 answer
  • For charles the great please help
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!