{"id":176,"date":"2026-11-13T12:33:37","date_gmt":"2026-11-13T04:33:37","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-fibonacci-search-splits-on-fibonacci-sizes\/"},"modified":"2026-11-13T21:51:51","modified_gmt":"2026-11-13T13:51:51","slug":"how-fibonacci-search-splits-on-fibonacci-sizes","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-fibonacci-search-splits-on-fibonacci-sizes\/","title":{"rendered":"How Fibonacci search splits on Fibonacci sizes"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Fibonacci search<\/strong> probes a sorted array on boundaries taken from the <strong>Fibonacci sequence<\/strong>, so each remaining window has a Fibonacci length and the next window is the previous Fibonacci number. The comparison count is <strong>O(log n)<\/strong>, the same order as binary search. Index arithmetic uses addition and subtraction of those numbers. The algorithm was attractive when division was the expensive part of computing a midpoint.<\/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 splits a window in half, which takes a division or a shift. Fibonacci numbers split a window into two smaller windows whose lengths are again Fibonacci numbers, so the same split repeats with no division. F(0) = 0, F(1) = 1, and F(k) = F(k\u22121)+F(k\u22122). A window of length F(k) probes at an offset of F(k\u22122), leaving a left window related to F(k\u22122) and a right window related to F(k\u22121).<\/p>\n\n\n<p class=\"wp-block-paragraph\">The comparisons are still comparisons. Nothing about the Fibonacci split discards more of the array than the midpoint does. The gain, where there is one, is in the index arithmetic and in access patterns that prefer a particular split.<\/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\">Let F(k) be the smallest Fibonacci number that is at least n. The search maintains a live window and a Fibonacci index. Each comparison reduces that index, so there are O(k) = O(log_\u03c6 n) comparisons, and \u03c6 is the golden ratio. Extra memory is a few integers. The result is the same lower-bound or equality answer binary search would return on the same sorted array, provided the discarded side is the side the comparison actually rules out.<\/p>\n\n\n<p class=\"wp-block-paragraph\">On a modern processor the division in lo+(hi\u2212lo)\/\/2 is not the cost of a search. Fibonacci search remains correct and remains logarithmic. It is not asymptotically faster than binary search, and the constant in the comparison count is not better.<\/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>Compute Fibonacci numbers until one is at least n. Remember the two preceding numbers.<\/li>\n<li>While the window is nonempty and the current Fibonacci number is greater than 1, probe at an offset of the earlier Fibonacci number from the window start, clamped to the last index.<\/li>\n<li>If the probed key is strictly smaller than the target, discard the prefix through the probe and step the Fibonacci pair down so the new window length matches the larger remaining part.<\/li>\n<li>If the probed key is strictly greater, discard the suffix from the probe and step the pair down to the smaller part.<\/li>\n<li>On an equal key, return that index, or keep the elimination going with a non-strict test when the caller needs the lower bound rather than any match.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The logarithmic bound is the sorted-array bound, reached by a different split than the midpoint. An unsorted array is still unsafe to split. A numeric uniform array that wants fewer than log n average probes is interpolation search, which uses the key values and gives up the worst case.<\/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 Fibonacci splits are treated as fewer comparisons<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Claim a log log n worst case<\/td>\n<td>Each probe discards a constant fraction. The depth is \u0398(log n), the same order as binary search.<\/td>\n<\/tr>\n<tr>\n<td>Probe at F(k\u22122) without clamping to n<\/td>\n<td>The Fibonacci number was rounded up past n. An offset past the last index is not an array element.<\/td>\n<\/tr>\n<tr>\n<td>Update only one side of the Fibonacci pair<\/td>\n<td>The next window must itself be a Fibonacci length, or the invariant that pays for the addition-only arithmetic is gone and the loop can skip the answer.<\/td>\n<\/tr>\n<tr>\n<td>Use it on a linked list to avoid division<\/td>\n<td>The probe still needs the item F(k\u22122) steps from the window start. A list charges that walk in full.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">General sorted-array search belongs to the half-open binary search. Division-free index arithmetic is a property of this split, and it is a reason to keep the algorithm only where that arithmetic shows up in the cost.<\/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 split the search window on Fibonacci sizes. Both print the index <code>5<\/code> of the value 13.<\/p>\n\n<pre><code class=\"language-bash\">a=(1 3 5 8 13 21 34)\ntarget=21\nn=${#a[@]}\nfib_m2=0\nfib_m1=1\nfib_m=$((fib_m2+fib_m1))\nwhile (( fib_m &lt; n )); do\n  fib_m2=$fib_m1\n  fib_m1=$fib_m\n  fib_m=$((fib_m2+fib_m1))\ndone\noffset=-1\nwhile (( fib_m &gt; 1 )); do\n  i=$(( offset + fib_m2 ))\n  if (( i &gt;= n )); then i=$((n-1)); fi\n  if (( a[i] &lt; target )); then\n    new_m=$fib_m1\n    new_m1=$fib_m2\n    new_m2=$((fib_m1 - fib_m2))\n    fib_m=$new_m\n    fib_m1=$new_m1\n    fib_m2=$new_m2\n    offset=$i\n  elif (( a[i] &gt; target )); then\n    new_m=$fib_m2\n    new_m1=$((fib_m1 - fib_m2))\n    new_m2=$((fib_m - fib_m1))\n    fib_m=$new_m\n    fib_m1=$new_m1\n    fib_m2=$new_m2\n  else\n    echo &quot;$i&quot;\n    exit 0\n  fi\ndone\nif (( fib_m1 &amp;&amp; offset + 1 &lt; n &amp;&amp; a[offset+1] == target )); then\n  echo $((offset+1))\nelse\n  echo -1\nfi\n<\/code><\/pre>\n<pre><code class=\"language-python\">def fib_search(a, target):\n    n = len(a)\n    fib_m2, fib_m1 = 0, 1\n    fib_m = fib_m2 + fib_m1\n    while fib_m &lt; n:\n        fib_m2, fib_m1 = fib_m1, fib_m\n        fib_m = fib_m2 + fib_m1\n    offset = -1\n    while fib_m &gt; 1:\n        i = min(offset + fib_m2, n - 1)\n        if a[i] &lt; target:\n            fib_m, fib_m1, fib_m2 = fib_m1, fib_m2, fib_m1 - fib_m2\n            offset = i\n        elif a[i] &gt; target:\n            fib_m, fib_m1, fib_m2 = fib_m2, fib_m1 - fib_m2, fib_m - fib_m1\n        else:\n            return i\n    if fib_m1 and offset + 1 &lt; n and a[offset + 1] == target:\n        return offset + 1\n    return -1\n\na = [1, 3, 5, 8, 13, 21, 34]\nassert fib_search(a, 21) == 5\nfor i, v in enumerate(a):\n    assert fib_search(a, v) == i\nprint(fib_search(a, 21))\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\">Fibonacci search splits a sorted array on Fibonacci sizes. It is uncommon beside binary search, which needs only a midpoint. The historical motive was a probe pattern that suited some storage, not a faster comparison count.<\/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 Fibonacci search on ordinary RAM in favor of binary search, which has a shorter proof and the same asymptotic comparison count. Keep it when the midpoint division is a real cost, or when an existing Fibonacci window is part of a larger structure that already holds those numbers.<\/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 Fibonacci search returns the Fibonacci window split and the log n bound it shares with binary search. <strong>Fibonacci search eliminates a Fibonacci-sized piece of a sorted array at each probe, which costs O(log n) comparisons and avoids a midpoint division.<\/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: Fibonacci search probes a sorted array on boundaries taken from the Fibonacci sequence, so each remaining window has a Fibonacci length and the next window is the previous Fibonacci number. The comparison count is O(log n), the same order as binary search. Index arithmetic uses addition and&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-176","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\/176","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=176"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/176\/revisions"}],"predecessor-version":[{"id":575,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/176\/revisions\/575"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}