Everyone Writes Ternary Search Suboptimally (Even tourist)

Revision en1, by cum00, 2022-07-24 05:35:05

When it comes to writing a ternary search, what you usually do is this:

repeat 30/60/100/200 times {
    double lm = l + (r - l) / 3;
    double rm = r - (r - l) / 3;
    if (f(lm) < f(rm))
        r = rm;
    else
        l = lm;
}

Sources (notice the authors): 154861075 154863973

I recommend everyone who still writes like this to read this article . It will improve your ability to fit nested ternary searches. Also, I hope I don't need to explain why it is strictly better than the shown code.

Please, refrain from writing in the comments "but still their code works and is accepted!!1!".

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English cum00 2022-07-24 05:46:13 11
en1 English cum00 2022-07-24 05:35:05 772 Initial revision (published)