{"id":590,"date":"2014-10-17T20:34:42","date_gmt":"2014-10-18T00:34:42","guid":{"rendered":"http:\/\/minireference.com\/blog\/?p=590"},"modified":"2014-10-17T20:34:42","modified_gmt":"2014-10-18T00:34:42","slug":"binary-search-in-three-languages","status":"publish","type":"post","link":"https:\/\/minireference.com\/blog\/binary-search-in-three-languages\/","title":{"rendered":"Binary search in three languages"},"content":{"rendered":"<p>Hola! Regardons ensemble un peu de code. The <code>binary_search<\/code> algorithm. It will get a little technical, pero no es mucho complicado. A ver. En ingles. En anglais, parce que le code&#8212;\u00e7a va foule mieux en anglais. <\/p>\n<p>Assume you&#8217;re given a array of integers sorted in increasing order <code>[3,6,19,21,87]<\/code>. You job is to write a &#8220;search&#8221; function that returns the 0-based index of query <code>val<\/code>ue, or <code>-1<\/code> if <code>val<\/code> is not in array.<\/p>\n<h3>Binary search algorithm<\/h3>\n<p>The &#8220;usual&#8221; algorithm <a href=\"http:\/\/rosettacode.org\/wiki\/Binary_search\">use the start and finish pointers in a weird way<\/a>, which I found difficult to understand, so I wrote another one. The invariant &#8220;we&#8217;ve already checked the limits&#8221; feels more logical to me.<\/p>\n<h3>In JavaScript<\/h3>\n<p>In <code>JavaScript<\/code> the code for the binary search strategy is as follows:<\/p>\n<pre>\nSearchableArray.prototype.binary_search = function (val) {\n    var data = this.data;\n\n    if (data.length === 0) return -1;\n    if (data[0] === val) return 0;\n    if (data[data.length-1] === val) return data.length -1 ;\n\n    var bin_search_limits = function(start,finish) {\n        \/\/ invariant: data[start] and data[finish] have been checked already\n        var mid;\n        \/\/console.log(start, finish);\n        if (start === finish || start+1 === finish) \n            return -1;\n\n        mid = start + Math.floor((finish-start)\/2);\n\n        if (data[mid]===val) {\n            return mid;\n        } else if (data[mid] < val) {\n            return bin_search_limits(mid,finish);\n        } else if (data[mid] > val) {\n            return bin_search_limits(start,mid);\n        }\n    };\n    return bin_search_limits(0, data.length-1);\n\n};\n<\/pre>\n<p>The full javascript code (with tests;) is <a href=\"https:\/\/bitbucket.org\/ivanistheone\/gab.js\/src\/tip\/algorithms\/js\/\">here<\/a>.<\/p>\n<h3>In C<\/h3>\n<p>It was surprisingly easy to transform the <code>JavaScript<\/code> code into <code>C<\/code>. See the code and some basic tests <a href=\"https:\/\/bitbucket.org\/ivanistheone\/gab.js\/src\/tip\/algorithms\/C\/\">here<\/a>. The main functions <a href=\"https:\/\/bitbucket.org\/ivanistheone\/gab.js\/src\/tip\/algorithms\/C\/binary_search.c#cl-27\">essentially the same<\/a>:<\/p>\n<pre>\nint bin_search_limits(int *data, int start, int finish, int val) {\n      \/\/ invariant: data[start] and data[finish] have been checked already\n      int mid;\n\n      if (start == finish || start+1 == finish) \n          return -1;\n\n      mid = start + (finish-start)\/2;  \n\n      if (data[mid]==val) {\n          return mid;\n      } else if (data[mid] < val) {\n          return bin_search_limits(data,mid,finish, val);\n      } else if (data[mid] > val) {\n          return bin_search_limits(data,start,mid, val);\n      }\n  };\n\nint binary_search(int *data, int length, int val) {\n  if (length == 0) return -1;\n  if (data[0] == val) return 0;\n  if (data[length-1] == val) return length-1;\n\n  return bin_search_limits(data,0,length-1, val);\n};\n<\/pre>\n<h3>In python<\/h3>\n<p>The pleasure of implementing binary search in python is left to the reader.<\/p>\n<hr\/>\n<p>I&#8217;ve got to go code learn how to make a hash function to make the C <a href=\"https:\/\/bitbucket.org\/ivanistheone\/gab.js\/src\/tip\/algorithms\/C\/README.txt#cl-62\">test suite go faster<\/a> \ud83d\ude09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hola! Regardons ensemble un peu de code. The binary_search algorithm. It will get a little technical, pero no es mucho complicado. A ver. En ingles. En anglais, parce que le code&#8212;\u00e7a va foule mieux en anglais. Assume you&#8217;re given a array of integers sorted in increasing order [3,6,19,21,87]. You job is to write a &#8220;search&#8221; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,5],"tags":[],"class_list":["post-590","post","type-post","status-publish","format-standard","hentry","category-coding","category-content"],"_links":{"self":[{"href":"https:\/\/minireference.com\/blog\/wp-json\/wp\/v2\/posts\/590","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/minireference.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/minireference.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/minireference.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/minireference.com\/blog\/wp-json\/wp\/v2\/comments?post=590"}],"version-history":[{"count":0,"href":"https:\/\/minireference.com\/blog\/wp-json\/wp\/v2\/posts\/590\/revisions"}],"wp:attachment":[{"href":"https:\/\/minireference.com\/blog\/wp-json\/wp\/v2\/media?parent=590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/minireference.com\/blog\/wp-json\/wp\/v2\/categories?post=590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/minireference.com\/blog\/wp-json\/wp\/v2\/tags?post=590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}