{"id":2840,"date":"2016-11-10T23:27:28","date_gmt":"2016-11-10T17:57:28","guid":{"rendered":"http:\/\/theoryofprogramming.azurewebsites.net\/?p=2840"},"modified":"2023-11-19T07:52:35","modified_gmt":"2023-11-19T07:52:35","slug":"jump-search-algorithm","status":"publish","type":"post","link":"https:\/\/theoryofcoding.com\/index.php\/2016\/11\/10\/jump-search-algorithm\/","title":{"rendered":"Jump Search Algorithm"},"content":{"rendered":"\n<p>Hello, people&#8230;! In this post, we will discuss a new search algorithm, Jump Search, which searches for an element in a sorted array in O(\u221aN) time. It is slower than binary search, then why learn it? What if the interviewer purposely asks you to write a search algorithm with O(\u221aN) worst case complexity? You would be banging your head to cook up a new algorithm \ud83d\ude1b . Anyways, it&#8217;s fun learning new stuff, isn&#8217;t it?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Algorithm<\/h2>\n\n\n\n<p>Imagine you are given a sorted array, and you are applying linear search to find a value. Jump Search is an improvement to this scenario. Instead of searching&nbsp;one-by-one, we search k-by-k. Let&#8217;s say we have a sorted array A, then jump search will look at A[1], A[1 + k], A[1 + 2k], A[1 + 3k] &#8230; and so on.<\/p>\n\n\n\n<p>As we keep jumping, we keep a note of the previous value and its index. Once we get a situation where A[i] &lt; searchValue &lt; A[i + k], then it means the value we want is in between the previous jump ends. So now we could simply perform a linear search between that A[i] and A[i + k].<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><a href=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/jump-search-1.jpg?ssl=1\"><img loading=\"lazy\" decoding=\"async\" width=\"1170\" height=\"502\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/jump-search-1.jpg?resize=1170%2C502&#038;ssl=1\" alt=\"Jump Search\" class=\"wp-image-2931\" srcset=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/jump-search-1.jpg?w=1422&amp;ssl=1 1422w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/jump-search-1.jpg?resize=300%2C129&amp;ssl=1 300w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/jump-search-1.jpg?resize=1024%2C439&amp;ssl=1 1024w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/jump-search-1.jpg?resize=768%2C329&amp;ssl=1 768w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/jump-search-1.jpg?resize=1000%2C429&amp;ssl=1 1000w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/jump-search-1.jpg?resize=230%2C99&amp;ssl=1 230w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/jump-search-1.jpg?resize=350%2C150&amp;ssl=1 350w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2016\/11\/jump-search-1.jpg?resize=480%2C206&amp;ssl=1 480w\" sizes=\"auto, (max-width: 1170px) 100vw, 1170px\" data-recalc-dims=\"1\" \/><\/a><\/figure>\n\n\n\n<p>Sound too boring? Here&#8217;s where things get interesting! How will we know the most optimal value of the jump size?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Optimal Jump Size<\/h2>\n\n\n\n<p>If we try to write down the complexity of the algorithm we described above.<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/ql-cache\/quicklatex.com-96f25e62e129efe0cdf9cf7212be19fa_l3.png?resize=81%2C19&#038;ssl=1\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#32;&#92;&#102;&#114;&#97;&#99;&#123;&#110;&#125;&#123;&#109;&#125;&#32;&#43;&#32;&#109;&#32;&#45;&#32;&#49;&#32;\" title=\"Rendered by QuickLaTeX.com\" height=\"19\" width=\"81\" style=\"vertical-align: -6px;\" data-recalc-dims=\"1\"\/><\/p>\n\n\n\n<p>Now, let&#8217;s see for which value of &#8216;m&#8217; does this expression have a minimum. To do this we differentiate with respect to &#8216;m&#8217; and equate it to 0<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/ql-cache\/quicklatex.com-8850a93d4308d5e38e2ff1a1e029f0e8_l3.png?resize=97%2C105&#038;ssl=1\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#32;&#92;&#102;&#114;&#97;&#99;&#123;&#110;&#125;&#123;&#32;&#45;&#109;&#94;&#123;&#50;&#125;&#32;&#125;&#32;&#43;&#32;&#49;&#32;&#61;&#32;&#48;&#32;&#92;&#110;&#101;&#119;&#108;&#105;&#110;&#101;&#32;&#92;&#110;&#101;&#119;&#108;&#105;&#110;&#101;&#32;&#110;&#32;&#61;&#32;&#109;&#94;&#123;&#50;&#125;&#32;&#92;&#110;&#101;&#119;&#108;&#105;&#110;&#101;&#32;&#92;&#110;&#101;&#119;&#108;&#105;&#110;&#101;&#32;&#109;&#32;&#61;&#32;&#92;&#115;&#113;&#114;&#116;&#123;&#110;&#125;&#32;\" title=\"Rendered by QuickLaTeX.com\" height=\"105\" width=\"97\" style=\"vertical-align: -4px;\" data-recalc-dims=\"1\"\/><\/p>\n\n\n\n<p>So, the optimal jump size is&nbsp;\u221aN.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Code<\/h2>\n\n\n\n<p>It is a&nbsp;pretty simple algorithm to code. Try it by yourself. You can refer to my code below if you get stuck \ud83d\ude42 .<\/p>\n\n\n<div class=\"su-tabs su-tabs-style-default su-tabs-mobile-stack\" data-active=\"1\" data-scroll-offset=\"0\" data-anchor-in-url=\"no\"><div class=\"su-tabs-nav\"><span class=\"\" data-url=\"\" data-target=\"blank\" tabindex=\"0\" role=\"button\">C<\/span><span class=\"\" data-url=\"\" data-target=\"blank\" tabindex=\"0\" role=\"button\">C++<\/span><span class=\"\" data-url=\"\" data-target=\"blank\" tabindex=\"0\" role=\"button\">Java<\/span><span class=\"\" data-url=\"\" data-target=\"blank\" tabindex=\"0\" role=\"button\">Python<\/span><\/div><div class=\"su-tabs-panes\"><div class=\"su-tabs-pane su-u-clearfix su-u-trim\" data-title=\"C\">\n<script src=\"https:\/\/gist.github.com\/VamsiSangam\/6dae4e952da326d81959425871866e75.js\"><\/script><br \/>\n<\/div>\n<div class=\"su-tabs-pane su-u-clearfix su-u-trim\" data-title=\"C++\">\n<script src=\"https:\/\/gist.github.com\/VamsiSangam\/870c9e1bd5280831979635a56a2099aa.js\"><\/script><br \/>\n<\/div>\n<div class=\"su-tabs-pane su-u-clearfix su-u-trim\" data-title=\"Java\">\n<script src=\"https:\/\/gist.github.com\/VamsiSangam\/aeaceec3f0c530fcecd785a43a6b665a.js\"><\/script><br \/>\n<\/div>\n<div class=\"su-tabs-pane su-u-clearfix su-u-trim\" data-title=\"Python\">\n<script src=\"https:\/\/gist.github.com\/VamsiSangam\/6b096d911d04034bbf99ad5422c10771.js\"><\/script><br \/>\n<\/div><\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Faster Jump Search<\/h2>\n\n\n\n<p>How can we make this version of jump search faster? A few people might have guessed it by now! \ud83d\ude09 In the above algorithm, once we find the jump ends which contains the value we need, we are performing a linear search. But we could perform another jump search between these two jump ends! And then recursively perform jump search until&nbsp;we are left with only one element.<\/p>\n\n\n\n<p>Implementing this version is more challenging. Firstly, you need to modify your above method to something which looks like this &#8211;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\npublic Pair jumpSearchUtil(int&#x5B;] arr, int val, int low, int high) {\n    \/\/ return jump ends if condition meets\n    \/\/ return null or a -1 otherwise\n}\n<\/pre><\/pre>\n\n\n\n<p>We must return the <em>i<\/em> and <em>j<\/em> between which the value is found&nbsp;or return null. And then call this from another method <em>jumpSearch()<\/em> which keeps calling <em>jumpSearchUtil()<\/em> until the jump interval has only 2 elements or it returns a null. We will need to return 2 values, the left end and the right end of the jump. So, we will use a structure in C, or a class in Java.<\/p>\n\n\n\n<p>Try to code this version of Jump Search algorithm.&nbsp;If you get stuck, you can use my code below as a reference &#8211;<\/p>\n\n\n<div class=\"su-tabs su-tabs-style-default su-tabs-mobile-stack\" data-active=\"1\" data-scroll-offset=\"0\" data-anchor-in-url=\"no\"><div class=\"su-tabs-nav\"><span class=\"\" data-url=\"\" data-target=\"blank\" tabindex=\"0\" role=\"button\">C<\/span><span class=\"\" data-url=\"\" data-target=\"blank\" tabindex=\"0\" role=\"button\">Java<\/span><\/div><div class=\"su-tabs-panes\"><div class=\"su-tabs-pane su-u-clearfix su-u-trim\" data-title=\"C\">\n<script src=\"https:\/\/gist.github.com\/VamsiSangam\/79c01544e139ccd2fcae30333373a9f9.js\"><\/script><br \/>\n<\/div>\n<div class=\"su-tabs-pane su-u-clearfix su-u-trim\" data-title=\"Java\">\n<script src=\"https:\/\/gist.github.com\/VamsiSangam\/a76d26658f470460e94517005165b067.js\"><\/script><br \/>\n<\/div><\/div><\/div>\n\n\n\n<p>This optimised version has the following complexity &#8211;<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/ql-cache\/quicklatex.com-53d2e084b4b7bd99ac740a7cb01d123c_l3.png?resize=263%2C32&#038;ssl=1\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#32;&#92;&#115;&#113;&#114;&#116;&#123;&#78;&#125;&#32;&#43;&#32;&#92;&#115;&#113;&#114;&#116;&#123;&#92;&#115;&#113;&#114;&#116;&#123;&#78;&#125;&#125;&#32;&#43;&#32;&#92;&#115;&#113;&#114;&#116;&#123;&#92;&#115;&#113;&#114;&#116;&#123;&#92;&#115;&#113;&#114;&#116;&#123;&#78;&#125;&#125;&#125;&#32;&#43;&#32;&#8230;&#32;&#43;&#32;&#49;&#32;\" title=\"Rendered by QuickLaTeX.com\" height=\"32\" width=\"263\" style=\"vertical-align: -7px;\" data-recalc-dims=\"1\"\/><br><br>This is still O(\u221aN) in the worst case. But it is much faster because, in the previous version, the complexity was \u221aN + \u221aN, which made it\u00a0O(\u221aN), but here the additional terms have very low values.<\/p>\n\n\n\n<p>I hope&nbsp;you&#8217;ve understood&nbsp;about the jump search algorithm. If you did, let me know by commenting! Keep practising! Happy Coding! \ud83d\ude00<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello, people&#8230;! In this post, we will discuss a new search algorithm, Jump Search, which searches for an element in a sorted [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[12],"tags":[69,99],"class_list":["post-2840","post","type-post","status-publish","format-standard","hentry","category-searching-algorithms","tag-jump-search-algorithm","tag-search-algorithms"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts\/2840","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/comments?post=2840"}],"version-history":[{"count":3,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts\/2840\/revisions"}],"predecessor-version":[{"id":3878,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts\/2840\/revisions\/3878"}],"wp:attachment":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/media?parent=2840"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/categories?post=2840"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/tags?post=2840"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}