{"id":197,"date":"2026-11-13T12:34:17","date_gmt":"2026-11-13T04:34:17","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-interpolation-search-places-the-next-probe\/"},"modified":"2026-11-13T21:52:33","modified_gmt":"2026-11-13T13:52:33","slug":"how-interpolation-search-places-the-next-probe","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-interpolation-search-places-the-next-probe\/","title":{"rendered":"How interpolation search places the next probe"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Interpolation search<\/strong> probes a sorted numeric array at the index the target would occupy if the keys between the ends were evenly spaced. Under independent uniform keys the expected number of probes is <strong>O(log log n)<\/strong>. On a skewed distribution the same formula walks the array almost one step at a time and the worst case is <strong>\u0398(n)<\/strong>. The even-spacing guess is the whole algorithm, and it is only as good as the distribution.<\/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\">Binary search always probes the middle, which is optimal when the only information is the comparison. Numeric keys carry a second piece of information: how far the target sits between the left and right key values. If those keys are spread evenly, the proportional index is near the answer, and repeating the estimate double-exponentially shrinks the range.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The formula divides by the gap between the end keys. A zero gap, a run of duplicates, or an exponential distribution makes the proportion meaningless. The algorithm then needs a rule that still terminates.<\/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\">The probe index is lo + (target \u2212 a[lo]) \u00b7 (hi \u2212 lo) \/ (a[hi] \u2212 a[lo]), computed in a type wide enough not to overflow, and clamped to the open interval between lo and hi. If the ends are equal, or the target lies outside them, the search stops or falls back to a binary step. Each probe discards one side, exactly as in binary search, so a sorted array is handled correctly even when the guess is poor.<\/p>\n\n\n<p class=\"wp-block-paragraph\">For independent uniform keys the expected probe count is O(log log n). There are sorted inputs, including geometrically increasing keys, on which the probe advances by O(1) and the cost is \u0398(n). Extra memory is a few indices. The algorithm does not replace binary search as a worst-case logarithmic method.<\/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>Set lo = 0 and hi = n\u22121 on a sorted numeric array. If n is 0, the key is absent.<\/li>\n<li>While lo \u2264 hi and the target lies between a[lo] and a[hi] inclusive, compute the proportional index and clamp it to [lo, hi].<\/li>\n<li>If a[pos] equals the target, return pos. If a[pos] is smaller, set lo = pos+1. Otherwise set hi = pos\u22121.<\/li>\n<li>If a[lo] equals a[hi], the proportion is undefined. Compare that key directly and stop.<\/li>\n<li>When a worst-case log n bound is required, take the probe as the median whenever the proportional index has not shrunk the range by a fixed fraction.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">Expected double-logarithmic time is a statement about uniform numeric keys. Strings, arbitrary comparable objects, and skewed numeric data do not inherit it. A library lower-bound on a sorted array should stay with binary search unless the distribution has been measured.<\/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 keys are not spread evenly<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Quote O(log log n) as a worst case<\/td>\n<td>Geometric keys and long duplicate runs produce a linear number of probes. The double logarithm is an expectation under uniformity.<\/td>\n<\/tr>\n<tr>\n<td>Divide by a[hi] \u2212 a[lo] when the ends are equal<\/td>\n<td>The proportion is undefined. The search has to stop or switch to a comparison step.<\/td>\n<\/tr>\n<tr>\n<td>Use the formula on a sorted array of strings<\/td>\n<td>Letter order does not make the code-point gap a uniform position estimate. Binary search uses the only operation the keys actually support.<\/td>\n<\/tr>\n<tr>\n<td>Omit the clamp on the computed index<\/td>\n<td>Integer division and a target outside the current ends can produce an index off the slice. The probe has to stay inside [lo, hi].<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">A worst-case logarithmic search belongs to binary search. A uniform spread that is being sorted, rather than searched, is the hypothesis behind bucket sort.<\/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 places the next probe where the target would sit if the values were linear between the ends. On this arithmetic progression the first probe is the answer, index <code>3<\/code>.<\/p>\n\n<pre><code class=\"language-python\">a = [10, 20, 30, 40, 50]\ntarget = 40\nlo, hi = 0, len(a) - 1\nwhile lo &lt;= hi and a[lo] &lt;= target &lt;= a[hi]:\n    if a[hi] == a[lo]:\n        pos = lo\n    else:\n        pos = lo + (target - a[lo]) * (hi - lo) \/\/ (a[hi] - a[lo])\n    if a[pos] == target:\n        print(pos)\n        break\n    if a[pos] &lt; target:\n        lo = pos + 1\n    else:\n        hi = pos - 1\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\">Interpolation search probes a sorted numeric array where the keys are spread evenly, the way a person looks up a name in a uniformly printed book. Skewed keys make the probe worse than binary search. Standard libraries binary-search unless they know the spread.<\/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 interpolation search when the key distribution is unknown or known to be skewed, and when a hard log n bound matters. Binary search is then the algorithm. Keep interpolation search for large sorted numeric arrays whose keys really are close to uniform.<\/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 interpolation search returns the proportional probe and the uniform hypothesis behind the log log n expectation. <strong>Interpolation search probes where a uniform spread would place the key, which is O(log log n) in expectation on uniform data and linear on a skewed sorted array.<\/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: Interpolation search probes a sorted numeric array at the index the target would occupy if the keys between the ends were evenly spaced. Under independent uniform keys the expected number of probes is O(log log n). On a skewed distribution the same formula walks the array almost&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-197","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\/197","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=197"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/197\/revisions"}],"predecessor-version":[{"id":596,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/197\/revisions\/596"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}