<?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[TechSphere]]></title><description><![CDATA[TechSphere is a hub for developers and learners, simplifying DSA, coding, AI, and cloud into clear insights to help you grow and build real solutions.]]></description><link>https://bubble-sort.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Mon, 12 Oct 2026 12:45:33 GMT</lastBuildDate><atom:link href="https://bubble-sort.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Bubble Sort]]></title><description><![CDATA[This blog covers:

Introduction

Step-by-step working of Bubble Sort

Dry Run (Example)

Code Implementation (in Python)

Optimizations

Merits

Demerits


🔄 Bubble Sort – A Step-by-Step Guide for Beginners
Sorting is one of the most fundamental ope...]]></description><link>https://bubble-sort.hashnode.dev/bubble-sort</link><guid isPermaLink="true">https://bubble-sort.hashnode.dev/bubble-sort</guid><category><![CDATA[bubble sort]]></category><category><![CDATA[sorting]]></category><category><![CDATA[sorting algorithms]]></category><dc:creator><![CDATA[Jeetesh Singh]]></dc:creator><pubDate>Tue, 09 Sep 2025 18:30:00 GMT</pubDate><content:encoded><![CDATA[<p>This blog covers:</p>
<ul>
<li><p>Introduction</p>
</li>
<li><p>Step-by-step working of Bubble Sort</p>
</li>
<li><p>Dry Run (Example)</p>
</li>
<li><p>Code Implementation (in Python)</p>
</li>
<li><p>Optimizations</p>
</li>
<li><p>Merits</p>
</li>
<li><p>Demerits</p>
</li>
</ul>
<h1 id="heading-bubble-sort-a-step-by-step-guide-for-beginners">🔄 Bubble Sort – A Step-by-Step Guide for Beginners</h1>
<p>Sorting is one of the most fundamental operations in computer science, and <strong>Bubble Sort</strong> is often the first algorithm introduced to beginners. While it's not the most efficient for large datasets, it's incredibly valuable for learning <strong>how sorting algorithms work</strong> under the hood.</p>
<p>In this blog, we’ll <strong>break down Bubble Sort step-by-step</strong> with examples, code, optimization tips, and its merits/demerits — all explained in simple language.</p>
<h2 id="heading-what-is-bubble-sort">📘 What is Bubble Sort?</h2>
<p><strong>Bubble Sort</strong> is a simple comparison-based sorting algorithm. It repeatedly <strong>swaps adjacent elements</strong> if they are in the wrong order.</p>
<p>Imagine bubbling up the largest element to the top, just like bubbles rising to the surface of water — that's how the name "Bubble Sort" came into play.</p>
<h2 id="heading-step-by-step-working-of-bubble-sort">🔍 Step-by-Step Working of Bubble Sort</h2>
<p>Let’s sort an array in <strong>ascending order</strong> using Bubble Sort:</p>
<p>Example:<br /><code>array = [5, 1, 4, 2, 8]</code></p>
<h3 id="heading-step-1-outer-loop-passes">🔄 Step 1: Outer Loop (Passes)</h3>
<p>The algorithm goes through the array <strong>N-1 times</strong> (N = number of elements), where each pass places the next largest element in its correct position.</p>
<h3 id="heading-step-2-inner-loop-comparisons">🔁 Step 2: Inner Loop (Comparisons)</h3>
<p>In each pass, it compares adjacent pairs (<code>arr[j]</code> and <code>arr[j+1]</code>):</p>
<ul>
<li><p>If <code>array[j] &gt; array[j+1]</code>, <strong>swap them</strong></p>
</li>
<li><p>Else, do nothing</p>
</li>
</ul>
<h3 id="heading-step-3-repeat-until-sorted">✅ Step 3: Repeat Until Sorted</h3>
<p>With each pass:</p>
<ul>
<li><p>The <strong>largest unsorted element</strong> moves to its correct position at the end</p>
</li>
<li><p>Remaining unsorted elements are processed in the next pass</p>
</li>
</ul>
<hr />
<h2 id="heading-dry-run-example">🧠 Dry Run (Example)</h2>
<p>Let’s dry-run <code>array = [5, 1, 4, 2, 8]</code> step-by-step:</p>
<h3 id="heading-pass-1">🔁 Pass 1:</h3>
<ul>
<li><p>Compare 5 and 1 → Swap → <code>[1, 5, 4, 2, 8]</code></p>
</li>
<li><p>Compare 5 and 4 → Swap → <code>[1, 4, 5, 2, 8]</code></p>
</li>
<li><p>Compare 5 and 2 → Swap → <code>[1, 4, 2, 5, 8]</code></p>
</li>
<li><p>Compare 5 and 8 → No swap → <code>[1, 4, 2, 5, 8]</code></p>
</li>
</ul>
<p>✅ Largest element (8) is in the correct position</p>
<h3 id="heading-pass-2">🔁 Pass 2:</h3>
<ul>
<li><p>Compare 1 and 4 → No swap</p>
</li>
<li><p>Compare 4 and 2 → Swap → <code>[1, 2, 4, 5, 8]</code></p>
</li>
<li><p>Compare 4 and 5 → No swap</p>
</li>
</ul>
<p>✅ Second largest (5) is now in place</p>
<h3 id="heading-pass-3">🔁 Pass 3:</h3>
<ul>
<li><p>Compare 1 and 2 → No swap</p>
</li>
<li><p>Compare 2 and 4 → No swap</p>
</li>
</ul>
<p>✅ No swaps – array is now sorted!</p>
<hr />
<h2 id="heading-python-code-bubble-sort">💻 Python Code – Bubble Sort</h2>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">bubble_sort</span>(<span class="hljs-params">arr</span>):</span>
    n = len(arr)
    <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(n):  <span class="hljs-comment"># Outer loop</span>
        <span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> range(<span class="hljs-number">0</span>, n - i - <span class="hljs-number">1</span>):  <span class="hljs-comment"># Inner loop</span>
            <span class="hljs-keyword">if</span> arr[j] &gt; arr[j + <span class="hljs-number">1</span>]:  <span class="hljs-comment"># Compare and swap</span>
                arr[j], arr[j + <span class="hljs-number">1</span>] = arr[j + <span class="hljs-number">1</span>], arr[j]
    <span class="hljs-keyword">return</span> arr

<span class="hljs-comment"># Example usage</span>
arr = [<span class="hljs-number">5</span>, <span class="hljs-number">1</span>, <span class="hljs-number">4</span>, <span class="hljs-number">2</span>, <span class="hljs-number">8</span>]
sorted_arr = bubble_sort(arr)
print(<span class="hljs-string">"Sorted array:"</span>, sorted_arr)
</code></pre>
<p>Output:</p>
<pre><code class="lang-plaintext">Sorted array: [1, 2, 4, 5, 8]
</code></pre>
<hr />
<h2 id="heading-optimized-bubble-sort">⚙️ Optimized Bubble Sort</h2>
<p>The regular version does unnecessary passes <strong>even if the array is already sorted</strong>.</p>
<p>Let’s optimize it by adding a <code>swapped</code> flag. If no elements were swapped in a pass, we can <strong>exit early</strong>.</p>
<h3 id="heading-optimized-python-code">✅ Optimized Python Code:</h3>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">optimized_bubble_sort</span>(<span class="hljs-params">arr</span>):</span>
    n = len(arr)
    <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(n):
        swapped = <span class="hljs-literal">False</span>
        <span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> range(<span class="hljs-number">0</span>, n - i - <span class="hljs-number">1</span>):
            <span class="hljs-keyword">if</span> arr[j] &gt; arr[j + <span class="hljs-number">1</span>]:
                arr[j], arr[j + <span class="hljs-number">1</span>] = arr[j + <span class="hljs-number">1</span>], arr[j]
                swapped = <span class="hljs-literal">True</span>
        <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> swapped:  <span class="hljs-comment"># No swap means array is sorted</span>
            <span class="hljs-keyword">break</span>
    <span class="hljs-keyword">return</span> arr

<span class="hljs-comment"># Example</span>
arr = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>]
sorted_arr = optimized_bubble_sort(arr)
print(<span class="hljs-string">"Sorted array:"</span>, sorted_arr)
</code></pre>
<p>✅ For already sorted or nearly sorted arrays, this is significantly faster.</p>
<hr />
<h2 id="heading-merits-of-bubble-sort">✅ Merits of Bubble Sort</h2>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Merit</td><td>Description</td></tr>
</thead>
<tbody>
<tr>
<td>🔹 Simple</td><td>Easy to understand and implement</td></tr>
<tr>
<td>🔹 In-place</td><td>Doesn’t require extra memory</td></tr>
<tr>
<td>🔹 Stable</td><td>Maintains the relative order of equal elements</td></tr>
<tr>
<td>🔹 Educational</td><td>Great for teaching basic sorting logic</td></tr>
</tbody>
</table>
</div><hr />
<h2 id="heading-demerits-of-bubble-sort">❌ Demerits of Bubble Sort</h2>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Demerit</td><td>Description</td></tr>
</thead>
<tbody>
<tr>
<td>🔸 Inefficient</td><td>Time complexity is <strong>O(n²)</strong> in worst and average case</td></tr>
<tr>
<td>🔸 Slow for large data</td><td>Not suitable for real-time or large-scale sorting</td></tr>
<tr>
<td>🔸 High number of swaps</td><td>More operations compared to efficient algorithms</td></tr>
</tbody>
</table>
</div><hr />
<h2 id="heading-time-and-space-complexity">📊 Time and Space Complexity</h2>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Case</td><td>Time Complexity</td></tr>
</thead>
<tbody>
<tr>
<td>Best (sorted)</td><td>O(n) <em>(with optimization)</em></td></tr>
<tr>
<td>Average</td><td>O(n²)</td></tr>
<tr>
<td>Worst</td><td>O(n²)</td></tr>
<tr>
<td>Space</td><td>O(1) <em>(no extra space needed)</em></td></tr>
</tbody>
</table>
</div><hr />
<h2 id="heading-final-thoughts">🧾 Final Thoughts</h2>
<p>Bubble Sort is <strong>not used in production systems</strong>, but it's still worth learning. It’s like <strong>learning alphabets before writing sentences</strong> — fundamental, simple, and educational.</p>
<p>Once you're comfortable with Bubble Sort, move on to faster algorithms like:</p>
<ul>
<li><p><strong>Selection Sort</strong></p>
</li>
<li><p><strong>Insertion Sort</strong></p>
</li>
<li><p><strong>Merge Sort</strong></p>
</li>
<li><p><strong>Quick Sort</strong></p>
</li>
</ul>
]]></content:encoded></item></channel></rss>