{"id":174,"date":"2026-11-13T12:33:33","date_gmt":"2026-11-13T04:33:33","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-exponential-search-brackets-a-nearby-key\/"},"modified":"2026-11-13T21:51:49","modified_gmt":"2026-11-13T13:51:49","slug":"how-exponential-search-brackets-a-nearby-key","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-exponential-search-brackets-a-nearby-key\/","title":{"rendered":"How exponential search brackets a nearby key"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Exponential search<\/strong> probes a sorted array at indices 1, 2, 4, 8, and so on, until the key at the probe is at least the target or the array ends. It then <strong>binary-searches<\/strong> the bracket that contains the answer. If the target sits at position p, the cost is <strong>O(log p)<\/strong> comparisons. The algorithm is binary search with a growing right end, useful on an unbounded sorted domain or when the hit is near the front.<\/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 needs both ends of the range. A sorted stream, an unbounded key space, or a huge array whose match is expected near the beginning does not hand you a tight right end. Doubling finds one. Once a[high] \u2265 target and a[high\/2] &lt; target, the ordinary half-open search runs on a range of length about p.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The same doubling is the gallop inside a timsort merge: one run has been winning, so the next loser is expected nearby, and a doubling probe finds how far to copy.<\/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 sorted random-access array, the doubling phase stops by index at most 2p, having made O(log p) probes. The following binary search costs O(log p) as well. An absent key, or a key past the end, costs O(log n). Extra memory is a few indices. Correctness is exactly binary search&#8217;s correctness on the bracket, plus the fact that a sorted array cannot hide a smaller key to the right of a key that is already \u2265 the target.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The base of the exponent can be any constant greater than 1. Base 2 is the usual choice. It does not improve the worst case over binary search on an array whose hit is uniformly random, because the expected position is then \u0398(n) and log p is log n.<\/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>If a[0] equals the target, return 0. If the array is empty or a[0] is already greater than the target, the lower bound is 0.<\/li>\n<li>Set bound = 1. While bound &lt; n and a[bound] &lt; target, double bound.<\/li>\n<li>Binary-search the half-open range [bound\/\/2, min(bound+1, n)). The left end was still strictly below the target, or it is the start of the array.<\/li>\n<li>Return the lower bound from that binary search.<\/li>\n<li>On an unbounded sorted generator, the same doubling runs until a produced key is \u2265 the target, then binary search is replaced by the same probe pattern inside the bracket.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The O(log p) bound needs sorted order and random access. An unsorted array, or a linked list, cannot skip to index 2^k. A uniform hit position removes the advantage over starting binary search on [0, n).<\/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 bracket is not actually a bracket<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Double until a[bound] equals the target and stop<\/td>\n<td>The first key that is \u2265 the target may overshoot a run of equals, and a miss never equals the target. The binary search inside the bracket returns the lower bound.<\/td>\n<\/tr>\n<tr>\n<td>Binary search from 0 to n when the point was a nearby hit<\/td>\n<td>The result is correct and the cost is log n. Doubling was how the cost was supposed to track p.<\/td>\n<\/tr>\n<tr>\n<td>Use the same probes on unsorted data<\/td>\n<td>a[bound] \u2265 target does not confine the answer to the bracket unless every earlier key is \u2264 every later key.<\/td>\n<\/tr>\n<tr>\n<td>Gallop one element at a time after the bracket is known<\/td>\n<td>That is linear search inside a range of length p. The second phase is binary search.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">A known finite sorted array with no reason to expect a small p belongs to ordinary binary search. The doubling probe inside a merge belongs to timsort&#8217;s gallop, which is this search on one run.<\/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\">The Bash script and the Python script exponential-search for a nearby key, then binary-search the bracket. Both print the index <code>3<\/code>.<\/p>\n\n<pre><code class=\"language-bash\">a=(1 3 5 7 9 11)\ntarget=7\nbound=1\nn=${#a[@]}\nwhile (( bound &lt; n &amp;&amp; a[bound] &lt; target )); do bound=$((bound*2)); done\nlo=$((bound\/2))\nhi=$(( bound + 1 &lt; n ? bound + 1 : n ))\nwhile (( lo &lt; hi )); do\n  mid=$(( lo + (hi - lo) \/ 2 ))\n  if (( a[mid] &lt; target )); then lo=$((mid+1))\n  else hi=$mid; fi\ndone\necho &quot;$lo&quot;\n<\/code><\/pre>\n<pre><code class=\"language-python\">a = [1, 3, 5, 7, 9, 11]\ntarget = 7\nif a[0] == target:\n    print(0)\nelse:\n    bound = 1\n    while bound &lt; len(a) and a[bound] &lt; target:\n        bound *= 2\n    lo, hi = bound \/\/ 2, min(bound + 1, len(a))\n    while lo &lt; hi:\n        mid = lo + (hi - lo) \/\/ 2\n        if a[mid] &lt; target:\n            lo = mid + 1\n        else:\n            hi = mid\n    assert a[lo] == target\n    print(lo)\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\">Exponential search finds a bound around a key in a sorted array of unknown or unbounded length, then hands that range to binary search. A caller that already knows the length skips the bracketing. The probes sit at 1, 2, 4, and so on.<\/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 exponential search when both ends are known and the target is not expected near one end. Binary search is simpler and has the same worst case. Keep it for unbounded sorted domains, for hits concentrated near the front, and as the probe inside a galloping merge.<\/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 exponential search returns the doubling bracket and the binary search that finishes it. <strong>Exponential search doubles an index until it brackets the target, then binary-searches a range whose length is proportional to the answer&#8217;s position.<\/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: Exponential search probes a sorted array at indices 1, 2, 4, 8, and so on, until the key at the probe is at least the target or the array ends. It then binary-searches the bracket that contains the answer. If the target sits at position p, the&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-174","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\/174","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=174"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/174\/revisions"}],"predecessor-version":[{"id":573,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/174\/revisions\/573"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}