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
Which statement correctly compares the function shown on this graph with the function y=6x-1
zavuch27 [327]

Answer:

D.

Step-by-step explanation:

The function shown on the graph is y = 5x - 6 and it is being comapred to y = 6x - 1. The function shown on the graph has a smaller rate of change because 5x < 6x. The starting point of the function shown on the graph is also lower since - 6 < - 1.

4 0
3 years ago
Read 2 more answers
What is the slope indicated in the table below: (x) 12,14,16,18 (y) 21,17,13,9
Nesterboy [21]
6345000000000000000000000000000000
7 0
3 years ago
What does 4 3/8+ 6 3/4 equal
ANTONII [103]

Answer: 11.125

Step-by-step explanation:

8 0
3 years ago
Read 2 more answers
The"B" isn't part of it, but how do I find x?
mr Goodwill [35]
The 2 angles form a linear pair and angles that form a linear pair have a sum of 180 so

10x + 15 + 5x = 180
15 + 15x = 180
180 - 15 = 15x
165 = 15x
11 = x
4 0
4 years ago
A student writes the equation for a line that has a slope of -6 and passes through the point (2, –8).
Ilia_Sergeevich [38]

Answer:

The error in the students work is

y -(-8) + 8 = -6x + 12 + 8

subtracting a negative is adding

y+8

They should subtract 8 from each side instead of adding 8 to each side

Step-by-step explanation:

We can use point slope form since we are given a point and the slope

y-y1 = m( x-x1)

y--8 = -6(x-2)

y+8 = -6(x-2)

Distribute the -6

y+8 = -6x+12

Subtract 8 from each side

y+8-8 = -6x+12-8

y = -6x+4

The error in the students work is

y -(-8) + 8 = -6x + 12 + 8

subtracting a negative is adding

y+8

They should subtract 8 from each side instead of adding 8 to each side

4 0
3 years ago
Read 2 more answers
Other questions:
  • The production constraints for a toy car and truck company are given by the equations and graph of the system of inequalities.
    14·2 answers
  • The decimal d is formed by writing in succession all the positive integers in increasing order after the decimal point; that is,
    6·1 answer
  • A TRAFFIC LIGHT IS RED FOR 38 SECONDS YELLOW FOR 10 SECONDS AND GREEN FOR ONE MINUTE AND TWELVE SEONDS. IN A CONSTANTLY REPEATIN
    12·1 answer
  • If you can buy 4⁄5 of a pound of ham for 3 dollars, how much can you purchase for 10 dollars? Write your answer as a fraction of
    12·1 answer
  • Jamie is buying tickets to a movie. How many $6 tickets can he buy if he wants to spend less than $36
    11·1 answer
  • Brainliest if correct just find the unit rate and plz be 100% sure when you give your answer cuz my grade kinda depends on this
    14·2 answers
  • Find the quotient 5/4 ➗1 1/12
    8·2 answers
  • What is the approximate area of the figure below?
    5·2 answers
  • A dripping tap wastes 11,250 ml of water in 9 hours. If it drips equal quantity in each hour ,how much water will be wasted in a
    5·1 answer
  • (x^2+3x+1)(x^2-2x+5)
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!