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]
2 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]2 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
Tresa soccer team I feel six water coolers before the game it's a water cooler hold 9 L to the water how many liters of water do
natima [27]
Well, 6 x 9 = 54 which means the answer is 54. Hope this helps :)
6 0
3 years ago
Read 2 more answers
8x2 - 6x + 3 – 2x – 10x?
Marina CMI [18]

Answer:

8x^{2} + 3 + -6x

Step-by-step explanation:

First we need to find the like terms so that we could add them. In this case the like terms are,

1. 6x , -2x , -10x

So, these are the only terms that we can add in this expression.

Hence,

=> 8x^{2} + 3 + (6x - 2x - 10x)

=> 8x^{2} + 3 + -6x

And this is as simple as this expression can go until we find the value of "x" which is not yet told.

Hope my answer helped.

4 0
2 years ago
Read 2 more answers
The amount of money left in Alma’s bank account is expressed by the linear equation y = -27x + 577, where x represents the numbe
marissa [1.9K]
145=-27x + 577
27x=432
x=16 weeks
6 0
3 years ago
Read 2 more answers
What is the point slope equation of the line with slope -12 that goes through the points (5, 3)?
Amanda [17]
The point-slope form of ay line is:

y-y1=m(x-x1), where m=slope and (x1,y1) is any point on the line.

In this case we are given that m=-12 and (x1,y1) is (5,3) so

y-3=-12(x-5)

8 0
3 years ago
Read 2 more answers
Evaluate 3{5 + 3[10 + 4 · 8]}
VMariaS [17]
393 is the answer to the question


8 0
3 years ago
Other questions:
  • Joni earned twice the amount of money babysitting on Saturday as she did on Wednesday. She earned $15 more on Friday than Wednes
    6·1 answer
  • Solve the compound inequality 6b &lt; 42 or 4b + 12 &gt; 8.
    9·1 answer
  • Write a statement that makes sense
    8·2 answers
  • Partial quotient of 235 divided by 5
    5·2 answers
  • Solve the system.<br> y= 3x2−2x+3​<br> y = x + 3<br><br> The solutions are (__,__) and (__,__).
    12·1 answer
  • In 2004, there were 19,396 bulldogs registered by the American Kennel Club. Approximately 86% of this number were registered in
    12·1 answer
  • Tanya has ten bills in her wallet. If she has one more $5 bill than $10 bills, and two more $1 bills than $5 bills, how many of
    6·1 answer
  • You have 4.5 pounds of egg whites. You need 8 oz to make one serving of consomme. How many can you
    5·1 answer
  • Can you form a right triangle form 21, 220, 221
    11·1 answer
  • two times a number is subtracted from 25 the result is the same as when it is added to six times.find the number​
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!