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
Help me help me help me help me help me help meHint hint wink wink there’s more than one answer
igor_vitrenko [27]
-1.08 because it is a decimal but includes a whole number other than the following fractions which are still in fractional form
7 0
3 years ago
What is the least common multiple of 6 and 2?​
Masteriza [31]

Answer:

6

Step-by-step explanation:

Hope I helped! Click the !

8 0
3 years ago
Read 2 more answers
What is the value of |45-75|
Masteriza [31]
The answer is 45 because I just had this question
6 0
4 years ago
Read 2 more answers
Question 23 of 40
aleksandrvk [35]

Answer:

B. The number of years that a fixed interest rate will be applied to the

loan

4 0
3 years ago
Mrs. Bautista invested Php2700.00 part at 8% and the rest at 11% .How musch did she invest at each rate if her total annual inco
Margarita [4]

Answer:

The answer to the question is

She invested

Php2700.00 at 8 % and

Php 20,400.00 at 11 %

Step-by-step explanation:

To solve the question we note that

Simple interest is given by \frac{P*R*T}{100} where

P= Principal, R = Rate and T = Time

If we call the first part P₁, T₁,  and R₁ and the second part

P₂, T₂,  and R₂

Then {P_1*(1+R_1*T_1}) +{P_*(1+R_2*T_2})  = 2460.00

= 2700×0.08×1 + P₂×0.11×1 = 2460  which gives

2244÷0.11 = P₂ or P₂ = Php 20,400.00

That is she invested

Php2700.00 at 8 % and

Php 20,400.00 at 11 %

5 0
3 years ago
Other questions:
  • Use the graph to write a linear function that relates y to x
    13·2 answers
  • A surveyor measures the angle of elevation at 20 degrees for a point 5 miles away. What is the vertical change in elevation from
    13·1 answer
  • If a dairyman has 300 pounds of milk testing 3% butterfat, how many pounds of skimmed milk (with no butterfat) must he remove to
    12·1 answer
  • What is the distance between the two points (6,-2) and (-4,7)
    7·1 answer
  • A shirt with a price of $24 was on sale for 30% off its original price. What
    10·1 answer
  • FIND THE OF P LEAVE YOUR ANSWER IN TERMS OF PU
    8·1 answer
  • How much did it cost trudy to get the loan for $500
    10·1 answer
  • Jose worked 3 hours.<br> In a one day what fraction<br> of the day did he work?
    15·1 answer
  • Solve for x , y , right answers plss !!!!
    14·1 answer
  • Please help! i can’t seem to find the answer to this question.
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!