<?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: Srinidhi Bandi</title>
    <description>The latest articles on DEV Community by Srinidhi Bandi (@srinidhi_bandi_).</description>
    <link>https://dev.to/srinidhi_bandi_</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%2F4167168%2F2b53317e-2481-4430-a4be-d8211d7e135e.jpg</url>
      <title>DEV Community: Srinidhi Bandi</title>
      <link>https://dev.to/srinidhi_bandi_</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/srinidhi_bandi_"/>
    <language>en</language>
    <item>
      <title>Why Binary Search has O(log n) Time Complexity?</title>
      <dc:creator>Srinidhi Bandi</dc:creator>
      <pubDate>Fri, 16 Oct 2026 16:33:12 +0000</pubDate>
      <link>https://dev.to/srinidhi_bandi_/why-binary-search-has-olog-n-time-complexity-2bb6</link>
      <guid>https://dev.to/srinidhi_bandi_/why-binary-search-has-olog-n-time-complexity-2bb6</guid>
      <description>&lt;p&gt;O said as Big O is used to express worst case time complexity. So log(n) is the worst case Time Complexity for the binary search algorithm. Before understanding why Binary Search has a log(n) Time Complexity, let us first see what is even a logarithm. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A logarithm is the power that a number needs to be raised to, to get some other number.&lt;/strong&gt;&lt;br&gt;
Sounds confusing? Let us understand with an example:-&lt;/p&gt;

&lt;p&gt;Let us take the number 8 into consideration. So we need to raise some number to some number to get 8 which we can visualize as:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;xˣ = 8&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In Computer Science, unless specified otherwise, we can always assume that the number that we want to raise to some power is &lt;strong&gt;2&lt;/strong&gt;. This number is called the &lt;strong&gt;base.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So, now my equation becomes &lt;strong&gt;2ˣ = 8.&lt;/strong&gt; This same equation can be written as &lt;strong&gt;log₂(8) = x.&lt;/strong&gt; Any guesses on the value of x?&lt;/p&gt;

&lt;p&gt;Yes, you got that right!!! The answer is 3. So &lt;strong&gt;log₂(8) = 3&lt;/strong&gt;. That means for n = 8 as the input for the logarithmic function with base 2, the answer comes out to be 3. So we can deduce that if there are 8 elements in the array, the binary search would take a maximum of 3 steps to find the number that you want to find which justifies the worst case scenario as well i.e. &lt;strong&gt;Time Complexity for a Binary Search Algorithm = O(log n).&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Dry run:&lt;br&gt;
Array: [10, 15, 30, 45, 60, 70, 100, 110]&lt;br&gt;
Target element: 10&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;: Start = 0, End = 7&lt;br&gt;
Mid = (Start + End) / 2 = (0 + 7) / 2 = 3&lt;br&gt;
Array[3] = 45&lt;/p&gt;

&lt;p&gt;Since 10 &amp;lt; 45, search the left half.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;: Start = 0, End = 2&lt;br&gt;
Mid = (0 + 2) / 2 = 1&lt;br&gt;
Array[1] = 15&lt;/p&gt;

&lt;p&gt;Since 10 &amp;lt; 15, search the left half.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;: Start = 0, End = 0&lt;br&gt;
Mid = (0 + 0) / 2 = 0&lt;br&gt;
Array[0] = 10&lt;/p&gt;

&lt;p&gt;Target element found at index 0 (position 1). As you noticed the algorithm took 3 steps to find our number which justifies our understanding of logarithmic complexity.&lt;/p&gt;

&lt;p&gt;I hope this blog helped you develop a better intuition for how algorithms work. If you have any questions, suggestions, or different approaches to solving a problem, feel free to share them in the comments. I'd love to hear your thoughts and learn from your experiences!&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>beginners</category>
      <category>computerscience</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
