{"id":266,"date":"2026-11-13T12:36:23","date_gmt":"2026-11-13T04:36:23","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-ternary-search-narrows-a-unimodal-function\/"},"modified":"2026-11-13T21:54:01","modified_gmt":"2026-11-13T13:54:01","slug":"how-ternary-search-narrows-a-unimodal-function","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-ternary-search-narrows-a-unimodal-function\/","title":{"rendered":"How ternary search narrows a unimodal function"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Ternary search<\/strong> finds the minimum of a <strong>unimodal<\/strong> function by comparing the function at the one-third and two-third points and discarding the third that cannot hold the minimum. Each step keeps <strong>2\/3<\/strong> of the interval, so the number of evaluations is <strong>\u0398(log n)<\/strong>, with two evaluations per step. On a discrete unimodal array, comparing two neighbors and binary-searching the slope uses one evaluation per step and shrinks the range by half.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">The function falls and then rises. Any point to the left of the minimum has a neighbor that is smaller on its right. Any point to the right has a neighbor that is smaller on its left. Two interior samples, ordered along the line, say which side of the minimum the search is on: the sample with the larger value is the side that can be discarded, out through that sample.<\/p>\n\n\n<p class=\"wp-block-paragraph\">This is not a faster way to find a key in a sorted array. Equality search already has binary search, which discards half the range with one comparison. Two comparisons that discard only one third are more work.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">On a continuous unimodal function, or on a discrete array that decreases and then increases, the larger of the two samples can be excluded, and the minimum remains inside the kept interval. After t steps the interval is (2\/3)^t of the original, so \u0398(log(1\/\u03b5)) evaluations reach a tolerance \u03b5, and \u0398(log n) evaluations finish a discrete range of length n. Extra memory is a few endpoints.<\/p>\n\n\n<p class=\"wp-block-paragraph\">If the function is only weakly unimodal, with a flat bottom, the same discards retain at least one minimum, and they may retain a whole flat run. A strictly unimodal function, decreasing up to a unique minimum and increasing after it, ends with that unique index. Golden-section search is the continuous variant that reuses one sample at the next step so only one new evaluation is required. The discard ratio is then the golden-section conjugate, about 0.618, which is a smaller shrink than 2\/3 per evaluation only in the accounting that charges the reused point fairly. On a discrete array the neighbor comparison is simpler and discards half.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Start with the full interval [lo, hi], and with a function that is unimodal on it.<\/li>\n<li>Set m1 and m2 one third and two thirds of the way from lo to hi. On an integer index range, use lo + (hi\u2212lo)\/\/3 and hi \u2212 (hi\u2212lo)\/\/3, and stop when the width is small.<\/li>\n<li>If f(m1) \u2264 f(m2), the minimum is at or left of m2. Set hi = m2. Otherwise set lo = m1.<\/li>\n<li>Repeat until the interval is a single index or is within the required tolerance.<\/li>\n<li>On a discrete array, prefer the slope test: if a[mid] \u2264 a[mid+1], the minimum is at or left of mid; otherwise it is at or right of mid+1. That is binary search and uses one probe.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The discard is valid for a unimodal function. A function with two valleys can hide the global minimum in the third that was dropped. A sorted array searched for a key is binary search. Nothing in the 2\/3 shrink beats a 1\/2 shrink that costs one comparison.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when the function is not unimodal<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Two valleys<\/td>\n<td>The samples can both lie on the slopes of the shallower valley. The deeper valley is discarded with the third that contained it.<\/td>\n<\/tr>\n<tr>\n<td>Ternary search for a key in a sorted array<\/td>\n<td>Each step spends two comparisons to keep 2\/3 of the range. Binary search spends one comparison to keep 1\/2, and finishes in fewer comparisons.<\/td>\n<\/tr>\n<tr>\n<td>Assume the discrete neighbor test is ternary search<\/td>\n<td>Comparing a[mid] with a[mid+1] is binary search on the slope. It is the better discrete algorithm and a different procedure.<\/td>\n<\/tr>\n<tr>\n<td>Reuse both sample points after the interval moves<\/td>\n<td>One of the two samples falls outside the kept interval or no longer sits at a third. Golden-section search is the reuse pattern that stays consistent, and plain ternary search recomputes both.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">Equality search on a sorted array belongs to binary search. A continuous unimodal search that must reuse evaluations belongs to golden-section search. The discrete slope test belongs with binary search.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment narrows a unimodal function with two probes and keeps the third that contains the minimum. f(x) = (x\u22123)\u00b2 on the integers 0\u20266 has its minimum at <code>3<\/code>.<\/p>\n\n<pre><code class=\"language-python\">def f(x):\n    return (x - 3) ** 2\n\nlo, hi = 0, 6\nwhile hi - lo &gt; 2:\n    left = lo + (hi - lo) \/\/ 3\n    right = hi - (hi - lo) \/\/ 3\n    if f(left) &lt; f(right):\n        hi = right\n    else:\n        lo = left\nbest = min(range(lo, hi + 1), key=f)\nassert best == 3\nprint(best)\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Ternary search finds the maximum of a unimodal function, such as a latency curve with a single valley, by dropping one third of the domain. Golden-section search is the continuous relative that libraries use more often. Newton&#8217;s method needs a derivative.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop ternary search on a discrete unimodal array in favor of the one-comparison neighbor test. Drop it entirely when the function may have more than one valley; a global optimization method is then required, and this discard is unsafe. Keep the two-probe ternary split for a unimodal function when both new evaluations are cheap to describe and the interval is continuous.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for ternary search returns the unimodal hypothesis and the reason it is the wrong tool on a sorted array. <strong>Ternary search discards one third of a unimodal interval after two evaluations, and a discrete unimodal array is better searched by comparing neighbors.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: Ternary search finds the minimum of a unimodal function by comparing the function at the one-third and two-third points and discarding the third that cannot hold the minimum. Each step keeps 2\/3 of the interval, so the number of evaluations is \u0398(log n), with two evaluations per&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,43],"tags":[],"class_list":["post-266","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-searching"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/266","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=266"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/266\/revisions"}],"predecessor-version":[{"id":665,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/266\/revisions\/665"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}