<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Decrepit Coder</title>
    <description>The latest articles on DEV Community by Decrepit Coder (@coderdecrepit).</description>
    <link>https://dev.to/coderdecrepit</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1093660%2F53788cef-7c7e-4c11-a769-22d7fd9af34f.png</url>
      <title>DEV Community: Decrepit Coder</title>
      <link>https://dev.to/coderdecrepit</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/coderdecrepit"/>
    <language>en</language>
    <item>
      <title>The Binary Search (Chop) Algorithm - with examples in C</title>
      <dc:creator>Decrepit Coder</dc:creator>
      <pubDate>Thu, 01 Jun 2023 14:31:33 +0000</pubDate>
      <link>https://dev.to/coderdecrepit/the-binary-search-chop-algorithm-with-examples-in-c-53eh</link>
      <guid>https://dev.to/coderdecrepit/the-binary-search-chop-algorithm-with-examples-in-c-53eh</guid>
      <description>&lt;p&gt;Recently, I decided to create my first instructional video on YouTube and after much deliberation, I eventually settled for the binary search (chop) algorithm. I know, it's all been done a thousand times before, but in my experience, most (if not all) tutorials don't really dig deep into the subtle nuances of its various implementations. Specifically, I wanted to cover both standard and reductive approaches, with particular attention given to both duplicate and nearest low/high matches.&lt;/p&gt;

&lt;p&gt;So without further ado, the video may be viewed &lt;a href="https://www.youtube.com/watch?v=VIswXkWOZoc&amp;amp;t=51s"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;As this is my first attempt at creating a video of any kind, I'd be very grateful for any comments regarding quality and/or content. In particular, I'd be grateful for any feedback regarding the quality of the narration - I decided to use IBM's text-to-speech service (Watson) rather than narrate the video myself.&lt;/p&gt;




&lt;p&gt;For posterity, the code used throughout the video is posted below.&lt;/p&gt;

&lt;p&gt;Version #1&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bool BinarySearch1(int pTarget,
                   int *pSortedArray,
                   int pArrayLength) {
  int topIndex, bottomIndex, compareIndex;

  bottomIndex = 0;
  topIndex = pArrayLength - 1; 

  while (topIndex &amp;gt;= bottomIndex) {
    compareIndex = (bottomIndex + topIndex) &amp;gt;&amp;gt; 1;

    if (pSortedArray[compareIndex] == pTarget)
      return true;

    if (pSortedArray[compareIndex] &amp;lt; pTarget)
      bottomIndex = compareIndex + 1;
    else
      topIndex = compareIndex - 1;
  }

  return false;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Version #2&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int BinarySearch2(int pTarget,
                  int *pSortedArray,
                  int pArrayLength,
                  bool pNearestLowest) {
  int topIndex, bottomIndex, compareIndex;

  bottomIndex = 0;
  topIndex = pArrayLength - 1;

  do {
    compareIndex = (bottomIndex + topIndex) &amp;gt;&amp;gt; 1;

    if (pSortedArray[compareIndex] == pTarget)
      return pSortedArray[compareIndex];

    if (pSortedArray[compareIndex] &amp;lt; pTarget)
      bottomIndex = compareIndex + 1;
    else
      topIndex = compareIndex - 1;

  } while (topIndex &amp;gt;= bottomIndex);

  return (pNearestLowest)
    ? pSortedArray[topIndex + (topIndex == -1)]
    : pSortedArray[bottomIndex - (bottomIndex == pArrayLength)];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Version #3&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int BinarySearch3(int pTarget,
                  int *pSortedArray,
                  int pArrayLength,
                  bool pNearestLower) {
  int topIndex, bottomIndex, compareIndex;

  bottomIndex = 0;
  topIndex = --pArrayLength;

  while (topIndex != bottomIndex) {
    compareIndex = (bottomIndex + topIndex) &amp;gt;&amp;gt; 1;

    if (pSortedArray[compareIndex] &amp;lt; pTarget)
      bottomIndex = compareIndex + 1;
    else
      topIndex = compareIndex;
  }

  return (pNearestLower)
    ? pSortedArray[bottomIndex - ((bottomIndex &amp;gt; 0) &amp;amp;&amp;amp;
                   (pSortedArray[bottomIndex] &amp;gt; pTarget))]
    : pSortedArray[bottomIndex + ((bottomIndex &amp;lt; pArrayLength) &amp;amp;&amp;amp;
                   (pSortedArray[bottomIndex] &amp;lt; pTarget))];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Version #4&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int BinarySearch4(int pTarget,
                  int *pSortedArray,
                  int pArrayLength,
                  bool pNearestLower) {
  int topIndex, bottomIndex, compareIndex;

  bottomIndex = 0;
  topIndex = --pArrayLength;

  while (topIndex != bottomIndex) {
    compareIndex = (bottomIndex + topIndex + 1) &amp;gt;&amp;gt; 1; 

    if (pSortedArray[compareIndex] &amp;gt; pTarget)
      topIndex = compareIndex - 1;
    else
      bottomIndex = compareIndex;
  }

  return (pNearestLower)
    ? pSortedArray[bottomIndex - ((bottomIndex &amp;gt; 0) &amp;amp;&amp;amp;
                   (pSortedArray[bottomIndex] &amp;gt; pTarget))]
    : pSortedArray[bottomIndex + ((bottomIndex &amp;lt; pArrayLength) &amp;amp;&amp;amp;
                   (pSortedArray[bottomIndex] &amp;lt; pTarget))];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>c</category>
      <category>cpp</category>
      <category>algorithms</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
