{"id":52,"date":"2021-03-01T01:33:57","date_gmt":"2021-03-01T01:33:57","guid":{"rendered":"http:\/\/danielpgleason.com\/?p=52"},"modified":"2021-03-01T02:12:24","modified_gmt":"2021-03-01T02:12:24","slug":"implementing-binary-search-in-c-practice","status":"publish","type":"post","link":"https:\/\/danielpgleason.com\/index.php\/2021\/03\/01\/implementing-binary-search-in-c-practice\/","title":{"rendered":"Implementing Binary Search in C++ Practice"},"content":{"rendered":"\n<p>So this is something we have all learned in a basic Computer Science course. However, I just wanted to write down a super simple implementation of Binary Search for people who are interested.  As you should already know, Binary Search can only be done on a sorted array. This function will only give you the index of the target in the given array. When first calling it, pass 0 for left ptr and array.size() &#8211; 1 for the right ptr.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-plane\"><code>int binarySearch(vector&lt;int&gt;& array, int target, int leftPtr, int rightPtr)\n{\n   int middle_index = (leftPtr + rightPtr) \/ 2;\n   int potentialMatch = array[middle_index];\n   if (target == potentialMatch)\n   {\n      return middle_index;\n   }\n   else if (target &gt; potentialMatch)\n   {\n      return binarySearch(array, target, middle_index + 1, rightPtr);\n   }\n   else\n   {\n      return binarySearch(array, target, leftPtr, middle_index - 1);\n   }\n}<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>So this is something we have all learned in a basic Computer Science course. However, I just wanted to write down a super simple implementation of Binary Search for people who are interested. As you should already know, Binary Search can only be done on a sorted array. This function will only give you the &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/danielpgleason.com\/index.php\/2021\/03\/01\/implementing-binary-search-in-c-practice\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Implementing Binary Search in C++ Practice&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-52","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/danielpgleason.com\/index.php\/wp-json\/wp\/v2\/posts\/52"}],"collection":[{"href":"https:\/\/danielpgleason.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/danielpgleason.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/danielpgleason.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/danielpgleason.com\/index.php\/wp-json\/wp\/v2\/comments?post=52"}],"version-history":[{"count":2,"href":"https:\/\/danielpgleason.com\/index.php\/wp-json\/wp\/v2\/posts\/52\/revisions"}],"predecessor-version":[{"id":54,"href":"https:\/\/danielpgleason.com\/index.php\/wp-json\/wp\/v2\/posts\/52\/revisions\/54"}],"wp:attachment":[{"href":"https:\/\/danielpgleason.com\/index.php\/wp-json\/wp\/v2\/media?parent=52"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/danielpgleason.com\/index.php\/wp-json\/wp\/v2\/categories?post=52"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/danielpgleason.com\/index.php\/wp-json\/wp\/v2\/tags?post=52"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}