<?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[Algorithms]]></title><description><![CDATA[All algorithms summerised]]></description><link>https://searchingalogs.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Tue, 22 Sep 2026 11:07:52 GMT</lastBuildDate><atom:link href="https://searchingalogs.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Searching Algo]]></title><description><![CDATA[Let’s dive into some of the most common searching algorithms, their pseudocode, and their time/space complexities:

1. Linear Search
Linear Search sequentially checks each element in the array until the target element is found or the end of the array...]]></description><link>https://searchingalogs.hashnode.dev/searching-algo</link><guid isPermaLink="true">https://searchingalogs.hashnode.dev/searching-algo</guid><category><![CDATA[coding]]></category><category><![CDATA[C++]]></category><category><![CDATA[Searching Algorithms]]></category><category><![CDATA[data structure and algorithms ]]></category><dc:creator><![CDATA[Shashi Shekhar]]></dc:creator><pubDate>Fri, 18 Oct 2024 18:29:25 GMT</pubDate><content:encoded><![CDATA[<p>Let’s dive into some of the most common searching algorithms, their pseudocode, and their time/space complexities:</p>
<hr />
<h3 id="heading-1-linear-search">1. <strong>Linear Search</strong></h3>
<p>Linear Search sequentially checks each element in the array until the target element is found or the end of the array is reached.</p>
<h4 id="heading-pseudocode">Pseudocode:</h4>
<pre><code class="lang-cpp"><span class="hljs-function">javaCopy <span class="hljs-title">codeLinearSearch</span><span class="hljs-params">(arr, target)</span>:
    <span class="hljs-keyword">for</span> i </span>= <span class="hljs-number">0</span> <span class="hljs-function">to <span class="hljs-title">length</span><span class="hljs-params">(arr)</span>-1:
        <span class="hljs-keyword">if</span> arr[i] </span>== target:
            <span class="hljs-keyword">return</span> i
    <span class="hljs-keyword">return</span> <span class="hljs-number">-1</span>  <span class="hljs-comment">// target not found</span>
</code></pre>
<h4 id="heading-time-complexity">Time Complexity:</h4>
<ul>
<li><p><strong>Best Case:</strong> O(1) (when the target is the first element)</p>
</li>
<li><p><strong>Average Case:</strong> O(n)</p>
</li>
<li><p><strong>Worst Case:</strong> O(n) (when the target is the last element or not present)</p>
</li>
</ul>
<h4 id="heading-space-complexity">Space Complexity:</h4>
<ul>
<li>O(1) (In-place search)</li>
</ul>
<hr />
<h3 id="heading-2-binary-search">2. <strong>Binary Search</strong></h3>
<p>Binary Search works on sorted arrays. It repeatedly divides the search interval in half. If the target value is smaller than the middle value, the search continues in the lower half, otherwise, it continues in the upper half.</p>
<h4 id="heading-pseudocode-recursive-version">Pseudocode (Recursive Version):</h4>
<pre><code class="lang-cpp"><span class="hljs-function">vbnetCopy <span class="hljs-title">codeBinarySearch</span><span class="hljs-params">(arr, target, low, high)</span>:
    <span class="hljs-keyword">if</span> low &gt; high:
        <span class="hljs-keyword">return</span> -1  <span class="hljs-comment">// target not found</span>
    mid </span>= (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">else</span> <span class="hljs-keyword">if</span> arr[mid] &gt; target:
        <span class="hljs-keyword">return</span> BinarySearch(arr, target, low, mid - <span class="hljs-number">1</span>)
    <span class="hljs-keyword">else</span>:
        <span class="hljs-keyword">return</span> BinarySearch(arr, target, mid + <span class="hljs-number">1</span>, high)
</code></pre>
<h4 id="heading-time-complexity-1">Time Complexity:</h4>
<ul>
<li><p><strong>Best Case:</strong> O(1) (when the middle element is the target)</p>
</li>
<li><p><strong>Average Case:</strong> O(log n)</p>
</li>
<li><p><strong>Worst Case:</strong> O(log n)</p>
</li>
</ul>
<h4 id="heading-space-complexity-1">Space Complexity:</h4>
<ul>
<li><p>O(log n) for recursive version (due to recursive call stack)</p>
</li>
<li><p>O(1) for iterative version</p>
</li>
</ul>
<p>The iterative version of Binary Search is space-efficient because it avoids the function call stack that the recursive version uses. This ensures a space complexity of <strong>O(1)</strong>.</p>
<p>Here's how the iterative binary search works:</p>
<hr />
<h3 id="heading-iterative-binary-search-pseudocode"><strong>Iterative Binary Search Pseudocode</strong></h3>
<pre><code class="lang-cpp"><span class="hljs-function">kotlinCopy <span class="hljs-title">codeIterativeBinarySearch</span><span class="hljs-params">(arr, target)</span>:
    low </span>= <span class="hljs-number">0</span>
    high = length(arr) - <span class="hljs-number">1</span>

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

        <span class="hljs-keyword">if</span> arr[mid] == target:
            <span class="hljs-keyword">return</span> mid  <span class="hljs-comment">// Target found at mid</span>
        elif arr[mid] &lt; target:
            low = mid + <span class="hljs-number">1</span>  <span class="hljs-comment">// Search the right half</span>
        <span class="hljs-keyword">else</span>:
            high = mid - <span class="hljs-number">1</span>  <span class="hljs-comment">// Search the left half</span>

    <span class="hljs-keyword">return</span> <span class="hljs-number">-1</span>  <span class="hljs-comment">// Target not found</span>
</code></pre>
<hr />
<h3 id="heading-explanation"><strong>Explanation:</strong></h3>
<ol>
<li><p><strong>Initialization</strong>: We initialize two pointers, <code>low</code> and <code>high</code>, to represent the current search boundaries in the sorted array.</p>
<ul>
<li><p><code>low = 0</code> (the start of the array).</p>
</li>
<li><p><code>high = length(arr) - 1</code> (the end of the array).</p>
</li>
</ul>
</li>
<li><p><strong>Loop</strong>: We repeatedly search within the bounds <code>low</code> and <code>high</code> until the range becomes invalid (i.e., <code>low &gt; high</code>), indicating that the target element is not present.</p>
</li>
<li><p><strong>Finding the Middle Element</strong>:</p>
<ul>
<li><p>We calculate the index of the middle element using <code>mid = low + (high - low) / 2</code>.</p>
</li>
<li><p>This formula is preferred over <code>(low + high) / 2</code> to avoid potential integer overflow.</p>
</li>
</ul>
</li>
<li><p><strong>Comparison</strong>:</p>
<ul>
<li><p>If <code>arr[mid]</code> is the target, return the index <code>mid</code>.</p>
</li>
<li><p>If the target is smaller than <code>arr[mid]</code>, adjust the search range to the left half by updating <code>high = mid - 1</code>.</p>
</li>
<li><p>If the target is larger than <code>arr[mid]</code>, adjust the search range to the right half by updating <code>low = mid + 1</code>.</p>
</li>
</ul>
</li>
<li><p><strong>Termination</strong>: The loop continues until <code>low &gt; high</code>, at which point the search space has been exhausted, and the target is not found.</p>
</li>
<li><p><strong>Space Efficiency</strong>: Since this version uses only a few extra variables (<code>low</code>, <code>high</code>, <code>mid</code>), the space complexity is <strong>O(1)</strong>.</p>
</li>
</ol>
<hr />
<h3 id="heading-time-complexity-2"><strong>Time Complexity:</strong></h3>
<ul>
<li><p><strong>Best Case</strong>: O(1) (if the target is found in the middle of the array on the first iteration).</p>
</li>
<li><p><strong>Worst and Average Case</strong>: O(log n) (since with each iteration, the search space is halved).</p>
</li>
</ul>
<h3 id="heading-space-complexity-2"><strong>Space Complexity:</strong></h3>
<ul>
<li><strong>O(1)</strong>: No recursion is used, and only a constant amount of extra space is needed for variables like <code>low</code>, <code>high</code>, and <code>mid</code>.</li>
</ul>
<hr />
<h3 id="heading-example-walkthrough">Example Walkthrough:</h3>
<p>Consider the sorted array <code>arr = [1, 3, 5, 7, 9, 11, 13, 15]</code>, and the target is <code>9</code>.</p>
<ol>
<li><p><code>low = 0</code>, <code>high = 7</code>, <code>mid = 3</code>.</p>
<ul>
<li><code>arr[3] = 7</code>, which is less than <code>9</code>. So, adjust <code>low = mid + 1 = 4</code>.</li>
</ul>
</li>
<li><p><code>low = 4</code>, <code>high = 7</code>, <code>mid = 5</code>.</p>
<ul>
<li><code>arr[5] = 11</code>, which is greater than <code>9</code>. So, adjust <code>high = mid - 1 = 4</code>.</li>
</ul>
</li>
<li><p><code>low = 4</code>, <code>high = 4</code>, <code>mid = 4</code>.</p>
<ul>
<li><code>arr[4] = 9</code>, which is equal to the target. Return <code>4</code>.</li>
</ul>
</li>
</ol>
<hr />
<h3 id="heading-3-jump-search">3. <strong>Jump Search</strong></h3>
<p>Jump Search is also designed for sorted arrays. The idea is to jump ahead by fixed steps (block size), and when the block where the target might be located is found, a linear search within the block is performed.</p>
<h4 id="heading-pseudocode-1">Pseudocode:</h4>
<pre><code class="lang-cpp"><span class="hljs-function">arduinoCopy <span class="hljs-title">codeJumpSearch</span><span class="hljs-params">(arr, target)</span>:
    n </span>= length(arr)
    step = <span class="hljs-built_in">sqrt</span>(n)
    prev = <span class="hljs-number">0</span>
    <span class="hljs-keyword">while</span> arr[min(step, n)<span class="hljs-number">-1</span>] &lt; target:
        prev = step
        step += <span class="hljs-built_in">sqrt</span>(n)
        <span class="hljs-keyword">if</span> prev &gt;= n:
            <span class="hljs-keyword">return</span> <span class="hljs-number">-1</span>  <span class="hljs-comment">// target not found</span>
    <span class="hljs-keyword">for</span> i = prev to min(step, n)<span class="hljs-number">-1</span>:
        <span class="hljs-keyword">if</span> arr[i] == target:
            <span class="hljs-keyword">return</span> i
    <span class="hljs-keyword">return</span> <span class="hljs-number">-1</span>  <span class="hljs-comment">// target not found</span>
</code></pre>
<h4 id="heading-time-complexity-3">Time Complexity:</h4>
<ul>
<li><p><strong>Best Case:</strong> O(1)</p>
</li>
<li><p><strong>Average Case:</strong> O(√n)</p>
</li>
<li><p><strong>Worst Case:</strong> O(√n)</p>
</li>
</ul>
<h4 id="heading-space-complexity-3">Space Complexity:</h4>
<ul>
<li>O(1) (In-place search)</li>
</ul>
<hr />
<h3 id="heading-4-interpolation-search">4. <strong>Interpolation Search</strong></h3>
<p>Interpolation Search is an improved variant of binary search for <strong>uniformly distributed</strong> data. Instead of dividing the array in half, it estimates the position of the target based on the values of the elements.</p>
<h4 id="heading-pseudocode-2">Pseudocode:</h4>
<pre><code class="lang-cpp"><span class="hljs-function">perlCopy <span class="hljs-title">codeInterpolationSearch</span><span class="hljs-params">(arr, target, low, high)</span>:
    <span class="hljs-keyword">while</span> low &lt;</span>= high <span class="hljs-keyword">and</span> target &gt;= arr[low] <span class="hljs-keyword">and</span> target &lt;= arr[high]:
        pos = low + ((target - arr[low]) * (high - low)) / (arr[high] - arr[low])
        <span class="hljs-keyword">if</span> arr[pos] == target:
            <span class="hljs-keyword">return</span> pos
        <span class="hljs-keyword">if</span> arr[pos] &lt; target:
            low = pos + <span class="hljs-number">1</span>
        <span class="hljs-keyword">else</span>:
            high = pos - <span class="hljs-number">1</span>
    <span class="hljs-keyword">return</span> <span class="hljs-number">-1</span>  <span class="hljs-comment">// target not found</span>
</code></pre>
<h4 id="heading-time-complexity-4">Time Complexity:</h4>
<ul>
<li><p><strong>Best Case:</strong> O(1)</p>
</li>
<li><p><strong>Average Case:</strong> O(log log n)</p>
</li>
<li><p><strong>Worst Case:</strong> O(n) (if the elements are not uniformly distributed)</p>
</li>
</ul>
<h4 id="heading-space-complexity-4">Space Complexity:</h4>
<ul>
<li>O(1) (In-place search)</li>
</ul>
<hr />
<h3 id="heading-5-exponential-search">5. <strong>Exponential Search</strong></h3>
<p>Exponential Search is used to search for an element in a <strong>sorted</strong> array. It works by increasing the search range exponentially, followed by a binary search in the found range.</p>
<h4 id="heading-pseudocode-3">Pseudocode:</h4>
<pre><code class="lang-cpp"><span class="hljs-function">lessCopy <span class="hljs-title">codeExponentialSearch</span><span class="hljs-params">(arr, target)</span>:
    <span class="hljs-keyword">if</span> arr[0] </span>== target:
        <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>
    i = <span class="hljs-number">1</span>
    <span class="hljs-keyword">while</span> i &lt; length(arr) <span class="hljs-keyword">and</span> arr[i] &lt;= target:
        i = i * <span class="hljs-number">2</span>
    <span class="hljs-keyword">return</span> BinarySearch(arr, target, i/<span class="hljs-number">2</span>, min(i, length(arr)<span class="hljs-number">-1</span>))
</code></pre>
<h4 id="heading-time-complexity-5">Time Complexity:</h4>
<ul>
<li><p><strong>Best Case:</strong> O(1)</p>
</li>
<li><p><strong>Average Case:</strong> O(log n)</p>
</li>
<li><p><strong>Worst Case:</strong> O(log n)</p>
</li>
</ul>
<h4 id="heading-space-complexity-5">Space Complexity:</h4>
<ul>
<li><p>O(log n) for binary search recursive version</p>
</li>
<li><p>O(1) for binary search iterative version</p>
</li>
</ul>
<hr />
<h3 id="heading-6-fibonacci-search">6. <strong>Fibonacci Search</strong></h3>
<p>Fibonacci Search works on <strong>sorted arrays</strong> and uses a Fibonacci sequence to divide the array. The idea is to eliminate large chunks of the array using Fibonacci numbers and reduce the search space.</p>
<h4 id="heading-pseudocode-4">Pseudocode:</h4>
<pre><code class="lang-cpp"><span class="hljs-function">javaCopy <span class="hljs-title">codeFibonacciSearch</span><span class="hljs-params">(arr, target)</span>:
    n </span>= length(arr)
    fibMMm2 = <span class="hljs-number">0</span>  <span class="hljs-comment">// (m-2)'th Fibonacci number</span>
    fibMMm1 = <span class="hljs-number">1</span>  <span class="hljs-comment">// (m-1)'th Fibonacci number</span>
    fibM = fibMMm2 + fibMMm1  <span class="hljs-comment">// m'th Fibonacci number</span>
    <span class="hljs-keyword">while</span> fibM &lt; n:
        fibMMm2 = fibMMm1
        fibMMm1 = fibM
        fibM = fibMMm2 + fibMMm1
    offset = <span class="hljs-number">-1</span>
    <span class="hljs-keyword">while</span> fibM &gt; <span class="hljs-number">1</span>:
        i = min(offset + fibMMm2, n<span class="hljs-number">-1</span>)
        <span class="hljs-keyword">if</span> arr[i] &lt; target:
            fibM = fibMMm1
            fibMMm1 = fibMMm2
            fibMMm2 = fibM - fibMMm1
            offset = i
        <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> arr[i] &gt; target:
            fibM = fibMMm2
            fibMMm1 = fibMMm1 - fibMMm2
            fibMMm2 = fibM - fibMMm1
        <span class="hljs-keyword">else</span>:
            <span class="hljs-keyword">return</span> i
    <span class="hljs-keyword">if</span> fibMMm1 <span class="hljs-keyword">and</span> arr[offset+<span class="hljs-number">1</span>] == target:
        <span class="hljs-keyword">return</span> offset+<span class="hljs-number">1</span>
    <span class="hljs-keyword">return</span> <span class="hljs-number">-1</span>  <span class="hljs-comment">// target not found</span>
</code></pre>
<h4 id="heading-time-complexity-6">Time Complexity:</h4>
<ul>
<li><p><strong>Best Case:</strong> O(1)</p>
</li>
<li><p><strong>Average Case:</strong> O(log n)</p>
</li>
<li><p><strong>Worst Case:</strong> O(log n)</p>
</li>
</ul>
<h4 id="heading-space-complexity-6">Space Complexity:</h4>
<ul>
<li>O(1) (In-place search)</li>
</ul>
<hr />
<h3 id="heading-7-ternary-search">7. <strong>Ternary Search</strong></h3>
<p>Ternary Search is a divide-and-conquer algorithm similar to binary search, but it divides the array into three parts and recursively searches in the relevant part.</p>
<h4 id="heading-pseudocode-5">Pseudocode:</h4>
<pre><code class="lang-cpp"><span class="hljs-function">kotlinCopy <span class="hljs-title">codeTernarySearch</span><span class="hljs-params">(arr, low, high, target)</span>:
    <span class="hljs-keyword">if</span> high &gt;</span>= low:
        mid1 = low + (high - low) / <span class="hljs-number">3</span>
        mid2 = high - (high - low) / <span class="hljs-number">3</span>
        <span class="hljs-keyword">if</span> arr[mid1] == target:
            <span class="hljs-keyword">return</span> mid1
        <span class="hljs-keyword">if</span> arr[mid2] == target:
            <span class="hljs-keyword">return</span> mid2
        <span class="hljs-keyword">if</span> target &lt; arr[mid1]:
            <span class="hljs-keyword">return</span> TernarySearch(arr, low, mid1 - <span class="hljs-number">1</span>, target)
        <span class="hljs-keyword">if</span> target &gt; arr[mid2]:
            <span class="hljs-keyword">return</span> TernarySearch(arr, mid2 + <span class="hljs-number">1</span>, high, target)
        <span class="hljs-keyword">return</span> TernarySearch(arr, mid1 + <span class="hljs-number">1</span>, mid2 - <span class="hljs-number">1</span>, target)
    <span class="hljs-keyword">return</span> <span class="hljs-number">-1</span>  <span class="hljs-comment">// target not found</span>
</code></pre>
<h4 id="heading-time-complexity-7">Time Complexity:</h4>
<ul>
<li><p><strong>Best Case:</strong> O(1)</p>
</li>
<li><p><strong>Average Case:</strong> O(log n)</p>
</li>
<li><p><strong>Worst Case:</strong> O(log n)</p>
</li>
</ul>
<h4 id="heading-space-complexity-7">Space Complexity:</h4>
<ul>
<li><p>O(log n) for recursive version (due to recursive call stack)</p>
</li>
<li><p>O(1) for iterative version</p>
</li>
</ul>
<hr />
<h3 id="heading-8-depth-first-search-dfs-for-graphtree-data-structures">8. <strong>Depth-First Search (DFS)</strong> (for graph/tree data structures)</h3>
<p>DFS explores as far as possible along each branch before backtracking. It uses a stack (either explicitly or via recursion) to track the current path.</p>
<h4 id="heading-pseudocode-for-a-graph">Pseudocode (for a graph):</h4>
<pre><code class="lang-cpp"><span class="hljs-function">sqlCopy <span class="hljs-title">codeDFS</span><span class="hljs-params">(graph, start)</span>:
    create a <span class="hljs-built_in">stack</span> S
    push start onto S
    mark start as visited
    <span class="hljs-keyword">while</span> S is <span class="hljs-keyword">not</span> empty:
        node </span>= S.pop()
        <span class="hljs-keyword">for</span> each neighbor of node:
            <span class="hljs-keyword">if</span> neighbor is <span class="hljs-keyword">not</span> visited:
                push neighbor onto S
                mark neighbor as visited
</code></pre>
<h4 id="heading-time-complexity-8">Time Complexity:</h4>
<ul>
<li><p><strong>Best Case:</strong> O(V + E) (where V is the number of vertices and E is the number of edges)</p>
</li>
<li><p><strong>Average Case:</strong> O(V + E)</p>
</li>
<li><p><strong>Worst Case:</strong> O(V + E)</p>
</li>
</ul>
<h4 id="heading-space-complexity-8">Space Complexity:</h4>
<ul>
<li>O(V) (due to recursion or stack space)</li>
</ul>
<hr />
<h3 id="heading-9-breadth-first-search-bfs-for-graphtree-data-structures">9. <strong>Breadth-First Search (BFS)</strong> (for graph/tree data structures)</h3>
<p>BFS explores all nodes at the present depth before moving on to the nodes at the next depth level. It uses a queue to track the next node to explore.</p>
<h4 id="heading-pseudocode-for-a-graph-1">Pseudocode (for a graph):</h4>
<pre><code class="lang-cpp"><span class="hljs-function">lessCopy <span class="hljs-title">codeBFS</span><span class="hljs-params">(graph, start)</span>:
    create a <span class="hljs-built_in">queue</span> Q
    enqueue start onto Q
    mark start as visited
    <span class="hljs-keyword">while</span> Q is <span class="hljs-keyword">not</span> empty:
        node </span>= Q.dequeue()
        <span class="hljs-keyword">for</span> each neighbor of node:
            <span class="hljs-keyword">if</span> neighbor is <span class="hljs-keyword">not</span> visited:
                enqueue neighbor onto Q
                mark neighbor as visited
</code></pre>
<h4 id="heading-time-complexity-9">Time Complexity:</h4>
<ul>
<li><p><strong>Best Case:</strong> O(V + E)</p>
</li>
<li><p><strong>Average Case:</strong> O(V + E)</p>
</li>
<li><p><strong>Worst Case:</strong> O(V + E)</p>
</li>
</ul>
<h4 id="heading-space-complexity-9">Space Complexity:</h4>
<ul>
<li>O(V) (due to the queue space)</li>
</ul>
]]></content:encoded></item></channel></rss>