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
PLEASE HELPPP!!!!
Salsk061 [2.6K]
40% of $3,200 is $ 1,280

william payed patio town $1,280 for the materials
8 0
3 years ago
What is the 40% of 60
forsale [732]

Answer:

24

Step-by-step explanation:

7 0
3 years ago
Read 2 more answers
Find the equation of a line in point slope form that crosses through points (3,-2) and (5,2)
madreJ [45]

-2-2=0.5(3-5)

OR (-2-2)=1/2(3-5)

3 0
3 years ago
Approximate, in feet, the circumference of a circle with radius = 6 ft. (Round your answer to one decimal place.)
Viktor [21]

Answer:

37.7

Step-by-step explanation:

Circumference of circle =2pir

I took pi=3.14

C=2xpix6=37.7square ft

8 0
3 years ago
What is 432*80? Show your work by multiplying with zeros
Dima020 [189]

Answer:

in the steps

Step-by-step explanation:

do 432*8 then just add a 0

3 0
3 years ago
Read 2 more answers
Other questions:
  • Angle b is a supplement of angle a and measure angle a = 65.2 degrees. find measure angle b
    15·1 answer
  • What is the lcm of two numbers if one number is a multiple of the other. Give an example
    14·1 answer
  • Find the volume of a cylinder with a diameter of 16 m and a height of 8 m.
    8·1 answer
  • You are to find the maximum and minimum value of the objective function in the domain
    14·1 answer
  • List all the subsets of {1}
    5·1 answer
  • A chef has less than 25.8 lb of rice. She takes out 5 lb to use during the day and stores the rest in containers. Each container
    8·1 answer
  • The length of the arc intercepted by a congruent central angle is proportional to the _______ in similar circles.
    13·2 answers
  • A house costs $600 more than 12 times the lot on which it was built. If the lot cost x dollars, what did the house cost? Which e
    6·1 answer
  • in 2000 the world's population was 6.08 billion and was increasing at a rate 1.21 each year. use the function to predict the pop
    10·1 answer
  • Can someone help me out?
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!