Ternary Search Algorithm

  1. Initialize Endpoints:
    • Set the left and right endpoints of the search range.
  2. Iterative Division:
    • Divide the search interval into three parts by calculating two midpoints, mid1 and mid2.
    • Determine the function values at mid1 and mid2.
  3. Update Endpoints:
    • Update the search range based on the function values at mid1 and mid2.
    • If the function value at mid1 is less than the value at mid2, update the left endpoint to mid1.
    • Otherwise, update the right endpoint to mid2.
  4. Termination:
    • Repeat steps 2-3 until the difference between the left and right endpoints is smaller than a predefined absolute precision.
  5. Result:
    • Return the midpoint between the final left and right endpoints as the x-coordinate of the maximum or minimum value.

Ternary Search in Python

Ternary Search is a searching technique used to determine the minimum or maximum of a Unimodal function. Ternary Search divided the search space into three parts and then remove one of the three parts to reduce the search space.

Similar Reads

Ternary Search Algorithm:

Initialize Endpoints:Set the left and right endpoints of the search range.Iterative Division:Divide the search interval into three parts by calculating two midpoints, mid1 and mid2.Determine the function values at mid1 and mid2.Update Endpoints:Update the search range based on the function values at mid1 and mid2.If the function value at mid1 is less than the value at mid2, update the left endpoint to mid1.Otherwise, update the right endpoint to mid2.Termination:Repeat steps 2-3 until the difference between the left and right endpoints is smaller than a predefined absolute precision.Result:Return the midpoint between the final left and right endpoints as the x-coordinate of the maximum or minimum value....

Ternary Search Illustration:

Consider the function f(x)=−(x−3)^2+5. We want to find the maximum value of this function within the range [0, 5]. Initialization:Set the left endpoint left=0 and the right endpoint right=5.Iterative Division:Calculate midpoints mid1 and mid2:mid1=left+3right−left​mid2=right−3right−left​Evaluate function values at mid1 and mid2:f(mid1)=f(1.666)=3.888f(mid2)=f(3.333)=4.555Update Endpoints:Since f(mid1)

Ternary Search Implementation:

Below is the implementation of Ternary Search in Python:...