{"id":202,"date":"2026-11-13T12:34:26","date_gmt":"2026-11-13T04:34:26","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-jump-search-blocks-a-sorted-array\/"},"modified":"2026-11-13T21:52:39","modified_gmt":"2026-11-13T13:52:39","slug":"how-jump-search-blocks-a-sorted-array","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-jump-search-blocks-a-sorted-array\/","title":{"rendered":"How jump search blocks a sorted array"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Jump search<\/strong> walks a sorted array in strides of length m and, once a stride overshoots the target, scans the previous block linearly. The cost is about <strong>n\/m + m<\/strong> comparisons. That sum is minimized at <strong>m = \u221an<\/strong>, which makes the search <strong>\u0398(\u221an)<\/strong>. On random-access memory, binary search&#8217;s log n is smaller. The block walk is the right shape when jumping backward is the expensive move.<\/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\">Suppose moving forward through the array is cheap and a backward seek is costly, as on a tape, or suppose a comparison is so cheap that the interesting cost is the number of blocks touched. Fixed strides use the sorted order without computing a midpoint. The stride length is a trade: a longer stride means fewer jumps and a longer linear cleanup.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The cleanup is necessary. The stride only finds a block that contains the lower bound. Items inside the block are not yet ordered relative to the probe in a way that would let you skip them, short of searching that block by a second method.<\/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\">With stride m, the jump phase reads at most \u2308n\/m\u2309 keys and the linear phase reads at most m\u22121 more. The sum \u2308n\/m\u2309 + m is \u0398(\u221an) when m is \u0398(\u221an), and the minimum over real m sits at \u221an. Extra memory is a few indices. Correctness is the sorted order: if a[i] &lt; target and i is a stride boundary, the answer is strictly after i, so every earlier block is discarded.<\/p>\n\n\n<p class=\"wp-block-paragraph\">Replacing the linear cleanup with a binary search inside the block gives a hybrid whose cost is one jump phase plus log m. That hybrid is no longer classical jump search, and on RAM it still loses to binary search over the whole array, which never spends the jump phase.<\/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>Choose m = floor(\u221an), and at least 1.<\/li>\n<li>Set i = 0. While i+m &lt; n and a[i+m] &lt; target, add m to i.<\/li>\n<li>The lower bound now lies in (i, min(i+m, n\u22121)], or at i if a[i] is already \u2265 the target.<\/li>\n<li>Scan that block linearly, or binary-search it, and return the first index whose key is \u2265 the target.<\/li>\n<li>If the scan passes the block with every key smaller, the target is absent and the lower bound is n.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The \u221an bound is a comparison count for a sorted array. It does not beat binary search on a machine where every index costs the same. It is the wrong tool for unsorted data, where no stride can be discarded.<\/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 stride is arbitrary<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Stride by a constant<\/td>\n<td>The jump count stays \u0398(n) and the algorithm has become a slower linear search.<\/td>\n<\/tr>\n<tr>\n<td>Stride by n\/2<\/td>\n<td>The cleanup scan is \u0398(n). One big jump does not locate a small block.<\/td>\n<\/tr>\n<tr>\n<td>Jump on an unsorted array<\/td>\n<td>a[i+m] &lt; target says nothing about the items skipped between i and i+m.<\/td>\n<\/tr>\n<tr>\n<td>Prefer it over binary search on RAM because \u221an looks simple<\/td>\n<td>log\u2082 n is smaller than \u221an for every array longer than a few dozen items. The tape argument is the one that keeps jump search.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">Equal-cost random access belongs to binary search. A hit expected near the front belongs to exponential search, whose cost tracks the position rather than \u221an.<\/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 jump-search with block length floor(sqrt(n)). Both print the index <code>6<\/code>.<\/p>\n\n<pre><code class=\"language-bash\">a=(0 1 1 2 3 5 8 13 21)\ntarget=8\nn=${#a[@]}\nstep=1\nwhile (( step * step &lt;= n )); do step=$((step+1)); done\nstep=$((step-1))\nif (( step &lt; 1 )); then step=1; fi\nprev=0\ncur=$step\nwhile (( a[cur &lt; n ? cur : n-1] &lt; target )); do\n  prev=$cur\n  cur=$((cur+step))\n  if (( prev &gt;= n )); then echo missing; exit 1; fi\ndone\nif (( cur &gt; n )); then cur=$n; fi\nidx=$prev\nwhile (( idx &lt; cur &amp;&amp; a[idx] &lt; target )); do idx=$((idx+1)); done\necho &quot;$idx&quot;\n<\/code><\/pre>\n<pre><code class=\"language-python\">a = [0, 1, 1, 2, 3, 5, 8, 13, 21]\ntarget = 8\nn = len(a)\nstep = int(n ** 0.5)\nprev = 0\nwhile a[min(step, n) - 1] &lt; target:\n    prev = step\n    step += int(n ** 0.5)\n    if prev &gt;= n:\n        raise SystemExit(&quot;missing&quot;)\nidx = prev\nwhile idx &lt; min(step, n) and a[idx] &lt; target:\n    idx += 1\nassert a[idx] == target\nprint(idx)\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\">Jump search scans a sorted array in fixed strides, which was a reasonable pattern when a sequential skip was cheaper than a random probe. Binary search replaced it for arrays in RAM. The stride idea remains in any blocked scan of a sorted file.<\/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 jump search on ordinary RAM. Binary search does fewer comparisons and has a simpler invariant. Keep it when forward motion is the natural access, backward motion is expensive, and a block of length \u221an is an acceptable linear cleanup.<\/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 jump search returns the \u221an stride and the reason binary search replaces it on random-access memory. <strong>Jump search strides by \u221an through a sorted array and scans one block, for \u0398(\u221an) comparisons, which random-access machines replace with binary search.<\/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: Jump search walks a sorted array in strides of length m and, once a stride overshoots the target, scans the previous block linearly. The cost is about n\/m + m comparisons. That sum is minimized at m = \u221an, which makes the search \u0398(\u221an). On random-access memory,&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-202","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\/202","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=202"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/202\/revisions"}],"predecessor-version":[{"id":601,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/202\/revisions\/601"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}