<?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: Deep Coomer</title>
    <description>The latest articles on DEV Community by Deep Coomer (@deep_coomer_45).</description>
    <link>https://dev.to/deep_coomer_45</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4001814%2Fb298e502-fc2a-4104-92b0-692ac78b4ddd.png</url>
      <title>DEV Community: Deep Coomer</title>
      <link>https://dev.to/deep_coomer_45</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/deep_coomer_45"/>
    <language>en</language>
    <item>
      <title>Why Repeatedly Halving an Array is O(log n), Not Linear</title>
      <dc:creator>Deep Coomer</dc:creator>
      <pubDate>Thu, 25 Jun 2026 08:11:50 +0000</pubDate>
      <link>https://dev.to/deep_coomer_45/why-repeatedly-halving-an-array-is-olog-n-not-linear-1lg3</link>
      <guid>https://dev.to/deep_coomer_45/why-repeatedly-halving-an-array-is-olog-n-not-linear-1lg3</guid>
      <description>&lt;p&gt;When you first learn about Binary Search, the explanation usually goes something like this: &lt;em&gt;"We look at the middle of the sorted array. If it's not our target, we throw away half the array and repeat."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;It's an incredibly efficient strategy. But if you sit down and try to map that action to Big-O notation, an intuitive trap opens up: &lt;/p&gt;

&lt;p&gt;&lt;em&gt;"If we are breaking the array in half, shouldn't the time complexity be O(n/2)&lt;/em&gt;&lt;em&gt;?"&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you’ve ever had this thought, you aren't alone. Today, let's look past the generic "just memorise the cheat sheet" advice and break down the exact math of why repeated halving completely changes the growth class of your code.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The Flaw in O(n/2)
&lt;/h2&gt;

&lt;p&gt;The immediate, "textbook" answer to this question is that Big-O notation ignores constant multipliers. Because of this, O(n/2) isn't actually a distinct category:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;O(n/2) = O(1/2 * n) = O(n)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An &lt;code&gt;O(n/2)&lt;/code&gt; algorithm is still &lt;strong&gt;linear&lt;/strong&gt;. If your dataset grows from 1,000 to 1,000,000 items, a linear algorithm's operations scale proportionally by 1,000x. &lt;/p&gt;

&lt;p&gt;But when we search a sorted array by halving it, we aren't just dividing the work into two piles and scanning one entire pile. &lt;strong&gt;We are repeating the division process at every single step.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. The Logic of Repeated Halving
&lt;/h2&gt;

&lt;p&gt;Let's step away from the abstract formulas and look at a concrete example. Imagine we have a sorted array of &lt;strong&gt;8 elements&lt;/strong&gt;, and we are looking for a specific number. &lt;/p&gt;

&lt;p&gt;Every time we check the middle element and miss, the search space shrinks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Step 0:&lt;/strong&gt; We start with &lt;strong&gt;8&lt;/strong&gt; elements.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 1:&lt;/strong&gt; First cut -&amp;gt; &lt;strong&gt;4&lt;/strong&gt; elements left.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 2:&lt;/strong&gt; Second cut -&amp;gt; &lt;strong&gt;2&lt;/strong&gt; elements left.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 3:&lt;/strong&gt; Third cut -&amp;gt; &lt;strong&gt;1&lt;/strong&gt; element left (we either found it or it's not there).
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[ 1, 2, 3, 4, 5, 6, 7, 8 ]  -&amp;gt; Step 0 (Size: 8)
      [ 5, 6, 7, 8 ]        -&amp;gt; Step 1 (Size: 4)
         [ 7, 8 ]           -&amp;gt; Step 2 (Size: 2)
           [ 8 ]            -&amp;gt; Step 3 (Size: 1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It exactly took us 3 steps to reduce 8 elements down to 1.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Connecting the Dots to Logarithms
&lt;/h2&gt;

&lt;p&gt;How do we mathematically describe the relationship between our starting size (8) and the number of steps it took to get to one (3)?&lt;/p&gt;

&lt;p&gt;We are asking: "How many times do we need to multiply 2 by itself to reach 8?"&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2 x 2 x 2 = 2^3 = 8
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The inverse of an exponent is a logarithm. Specifically, a base-2 logarithm &lt;code&gt;(log_2)&lt;/code&gt; answers the exact opposite question: "How many times do I have to divide this number by 2 to get down to 1?"&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;log_2(8) = 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we scale this up to an array of any size n, the number of steps k required to finish the search follows this formula:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;n/2^k = 1 =&amp;gt; n = 2^k =&amp;gt; k = log_2(n)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Why Big-O Drops the Log Base
&lt;/h2&gt;

&lt;p&gt;You might wonder why we write &lt;code&gt;O(log n)&lt;/code&gt; instead of &lt;code&gt;O(log_2 n)&lt;/code&gt;, especially since we are dividing by 2. In computer science, base-2 is the default assumption, but mathematically, the base actually doesn't change the Big-O classification.&lt;/p&gt;

&lt;p&gt;Thanks to the &lt;strong&gt;Change of Base rule&lt;/strong&gt;, converting a base-2 log to a standard base-10 log looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;log_2(n) = log_10(n) / log_10(2) ≈ 3.32 * log_10(n)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because &lt;code&gt;log_10(2)&lt;/code&gt; is just a constant number (≈ &lt;code&gt;0.301&lt;/code&gt;), &lt;code&gt;log_2(n)&lt;/code&gt; and &lt;code&gt;log_10(n)&lt;/code&gt; differ only by a constant multiplier (&lt;code&gt;3.32&lt;/code&gt;).. Just like dropping the &lt;code&gt;1/2&lt;/code&gt; from &lt;code&gt;O(n/2)&lt;/code&gt;, Big-O drops this multiplier too:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;O(log_2 n) = O(3.32 * log_10 n) = O(log_n)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Whether you are dividing your data into 2 parts (Binary Search) or 10 parts (like a 10-way B-Tree search), the algorithm scales at the exact same logarithmic rate.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary Comparison
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Concept&lt;/th&gt;
&lt;th&gt;Mechanics&lt;/th&gt;
&lt;th&gt;Big-O Class&lt;/th&gt;
&lt;th&gt;Scaling Behavior&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;O(n/2)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;A single scan over exactly half of the data.&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;O(n) (Linear)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;If data doubles, execution time &lt;strong&gt;doubles&lt;/strong&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;O(\log n)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Data is repeatedly cut in half at every step.&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;O(\log n) (Logarithmic)&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;If data doubles, you only add &lt;strong&gt;one extra step&lt;/strong&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The Ultimate Perspective:&lt;/strong&gt;&lt;br&gt;
If you have a sorted array of &lt;strong&gt;1,000,000 elements&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A linear O(n/2) approach takes &lt;strong&gt;500,000 operations&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;A logarithmic O(\log n) approach takes roughly &lt;strong&gt;20 operations&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;The next time someone tells you an algorithm is efficient because it "cuts the problem in half," remember that the magic isn't in the first cut—it's in the power of the &lt;em&gt;repeated&lt;/em&gt; cut.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thanks for reading! If you found this breakdown helpful, drop a comment below with the computer science concept that took you the longest to intuitively click.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>computerscience</category>
      <category>datastructures</category>
      <category>performance</category>
    </item>
  </channel>
</rss>
