<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[ShivanAnand]]></title><description><![CDATA[ShivanAnand]]></description><link>https://shivananand.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sun, 11 Oct 2026 12:06:03 GMT</lastBuildDate><atom:link href="https://shivananand.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Binary Search in Python]]></title><description><![CDATA[Let’s go deep into Binary Search so that you don’t just “know the code“, you understand exactly why every line exists and what’s happening in your brain when you run it.
The Big Idea
Binary search is a divide and conquer algorithm for searching in a ...]]></description><link>https://shivananand.hashnode.dev/binary-search-in-python</link><guid isPermaLink="true">https://shivananand.hashnode.dev/binary-search-in-python</guid><category><![CDATA[Binary Search Algorithm]]></category><category><![CDATA[Python]]></category><category><![CDATA[algorithms]]></category><dc:creator><![CDATA[SHIVAN ANAND]]></dc:creator><pubDate>Fri, 15 Aug 2025 14:27:49 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1755269961094/205758a1-24a8-4d57-ba40-c05c5e243e47.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Let’s go deep into <strong>Binary Search</strong> so that you don’t just “know the code“, you understand exactly why every line exists and what’s happening in your brain when you run it.</p>
<h1 id="heading-the-big-idea">The Big Idea</h1>
<p>Binary search is a <strong>divide and conquer</strong> algorithm for <strong>searching</strong> in a <strong>sorted</strong> list.</p>
<ul>
<li><p>If the list is <strong>not sorted</strong>, binary search won’t work (we’ll see why).</p>
</li>
<li><p>It repeatedly <strong>splits the search space in half</strong> until it finds the element or runs out of places to look.</p>
</li>
</ul>
<p>Think of it like:</p>
<blockquote>
<p>You’re looking for a word in a dictionary. You don’t flip one page at a time; you open near the middle, check, then decide which half to keep.</p>
</blockquote>
<h1 id="heading-why-sorted">Why Sorted?</h1>
<p>Let’s say you’re looking for <code>45</code> in:</p>
<pre><code class="lang-python">[<span class="hljs-number">10</span>, <span class="hljs-number">15</span>, <span class="hljs-number">20</span>, <span class="hljs-number">25</span>, <span class="hljs-number">30</span>, <span class="hljs-number">35</span>, <span class="hljs-number">40</span>, <span class="hljs-number">45</span>, <span class="hljs-number">50</span>, <span class="hljs-number">55</span>]
</code></pre>
<p>If it’s sorted:</p>
<ul>
<li><p>You can check the <strong>middle</strong> and decide whether to go <strong>left</strong> or <strong>right</strong> because you know all left elements are smaller, all right elements are bigger.</p>
</li>
<li><p>This “direction“ is possible <strong>only</strong> if the list is sorted.</p>
</li>
</ul>
<p>If the list is unsorted:</p>
<pre><code class="lang-python">[<span class="hljs-number">45</span>, <span class="hljs-number">10</span>, <span class="hljs-number">55</span>, <span class="hljs-number">30</span>, <span class="hljs-number">20</span>]
</code></pre>
<p>Checking the middle tells you nothing — you’d have to search linearly.</p>
<h1 id="heading-the-steps-logic">The Steps (Logic)</h1>
<p>Let’s write it in <strong>human language</strong> first:</p>
<ol>
<li><p><strong>Start</strong> with the entire range:</p>
<p> <code>low = 0</code>, <code>high = len(list) - 1</code></p>
</li>
<li><p><strong>While</strong> there’s still a range left (<code>low &lt;= high</code>):</p>
<ul>
<li><p>Find the middle index:</p>
<p>  <code>mid = (low + high) // 2</code></p>
<p>  (integer division so we don’t get fractions)</p>
</li>
<li><p>If <code>arr[mid] == target</code>: 🎯 Found it! Return the index.</p>
</li>
<li><p>If <code>arr[mid] &lt; target</code>:</p>
<p>  → Target is in the <strong>right half</strong>. Move <code>low</code> to <code>mid + 1</code>.</p>
</li>
<li><p>If <code>arr[mid] &gt; target</code>:</p>
<p>  → Target is in the <strong>left half</strong>. Move <code>high</code> to <code>mid - 1</code>.</p>
</li>
</ul>
</li>
<li><p>If loop ends → Target not found, return something like <code>-1</code>.</p>
</li>
</ol>
<h1 id="heading-the-why-of-mid-low-high-2">The “Why“ of <code>mid = (low + high) // 2</code></h1>
<p>We take the middle to <strong>cut the search space in half</strong> each time:</p>
<ul>
<li><p>If you start with 1,000,000 items:</p>
<ul>
<li><p>1st step: check middle → ~500,000 left to search.</p>
</li>
<li><p>2nd step: ~250,000 left.</p>
</li>
<li><p>3rd: ~125,000.</p>
</li>
<li><p>And so on…</p>
<p>  This is why binary search is <strong>O(log n)</strong> in time complexity.</p>
</li>
</ul>
</li>
</ul>
<h1 id="heading-python-code-iterative-version">Python Code (Iterative Version)</h1>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">binary_search</span>(<span class="hljs-params">arr, target</span>):</span>
    low = <span class="hljs-number">0</span>
    high = len(arr) - <span class="hljs-number">1</span>

    <span class="hljs-keyword">while</span> low &lt;= high:
        mid = (low + high) // <span class="hljs-number">2</span>

        <span class="hljs-keyword">if</span> arr[mid] == target:
            <span class="hljs-keyword">return</span> mid  <span class="hljs-comment"># Found at index mid</span>

        <span class="hljs-keyword">elif</span> arr[mid] &lt; target:
            low = mid + <span class="hljs-number">1</span>  <span class="hljs-comment"># Search right half</span>

        <span class="hljs-keyword">else</span>:
            high = mid - <span class="hljs-number">1</span> <span class="hljs-comment"># Search left half</span>

    <span class="hljs-keyword">return</span> <span class="hljs-number">-1</span>  <span class="hljs-comment"># Not found</span>
</code></pre>
<h1 id="heading-python-code-recursive-version">Python Code (Recursive Version)</h1>
<p>For completeness — recursion just means “function calling itself on the smaller half“.</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">binary_search_recursive</span>(<span class="hljs-params">arr, target, low, high</span>):</span>
    <span class="hljs-keyword">if</span> low &gt; high:
        <span class="hljs-keyword">return</span> <span class="hljs-number">-1</span> <span class="hljs-comment"># Not found</span>

    mid = (low + high) // <span class="hljs-number">2</span>

    <span class="hljs-keyword">if</span> arr[mid] == target:
        <span class="hljs-keyword">return</span> mid
    <span class="hljs-keyword">elif</span> arr[mid] &lt; target:
        <span class="hljs-keyword">return</span>  binary_search_recursive(arr, target, mid + <span class="hljs-number">1</span>, high)
    <span class="hljs-keyword">else</span>:
        <span class="hljs-keyword">return</span> binary_search_recursive(arr, target, low, mid - <span class="hljs-number">1</span>)
</code></pre>
<p>Call it like:</p>
<pre><code class="lang-python">arr = [<span class="hljs-number">10</span>, <span class="hljs-number">15</span>, <span class="hljs-number">20</span>, <span class="hljs-number">25</span>, <span class="hljs-number">30</span>, <span class="hljs-number">35</span>, <span class="hljs-number">40</span>, <span class="hljs-number">45</span>, <span class="hljs-number">50</span>, <span class="hljs-number">55</span>]
print(binary_search_recursive(arr, <span class="hljs-number">45</span>, <span class="hljs-number">0</span>, len(arr) - <span class="hljs-number">1</span>))
</code></pre>
<h1 id="heading-complexity">Complexity</h1>
<ul>
<li><p><strong>Time Complexity:</strong></p>
<ul>
<li><p>Best case: O(1) (found at first try)</p>
</li>
<li><p>Worst/Average case: O(log n)</p>
</li>
</ul>
</li>
<li><p><strong>Space Complexity:</strong></p>
<ul>
<li><p>Iterative: O(1)</p>
</li>
<li><p>Recursive: O(log n) (stack frames for recursion)</p>
</li>
</ul>
</li>
</ul>
<h1 id="heading-common-pitfalls">Common Pitfalls</h1>
<ul>
<li><p><strong>Forgetting to sort the list first.</strong></p>
</li>
<li><p><strong>Infinite loops</strong> when <code>low</code> or <code>high</code> doesn’t move properly.</p>
</li>
<li><p><strong>Off-by-one errors</strong> in the index calculations.</p>
</li>
<li><p><strong>Integer overflow</strong> for <code>mid = (low + high) // 2</code> in some languages — Python is safe because integers are big, but in C/C++ we often use <code>mid = low + (high - low) // 2</code>.</p>
</li>
</ul>
]]></content:encoded></item></channel></rss>