<?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[Prateek's Blog]]></title><description><![CDATA[Prateek's Blog]]></description><link>https://blog.prateekroy.com</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1690822519708/lusXkRxaW.png</url><title>Prateek&apos;s Blog</title><link>https://blog.prateekroy.com</link></image><generator>RSS for Node</generator><lastBuildDate>Fri, 08 May 2026 23:20:29 GMT</lastBuildDate><atom:link href="https://blog.prateekroy.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Bubble Sort]]></title><description><![CDATA[Bubble sort is a simple sorting algorithm that relies on comparing adjacent elements and swapping them if they are in the wrong order. In each iteration, the elements "bubble up" to their correct position and the process is repeated until the entire ...]]></description><link>https://blog.prateekroy.com/bubble-sort</link><guid isPermaLink="true">https://blog.prateekroy.com/bubble-sort</guid><category><![CDATA[data structures]]></category><category><![CDATA[bubble sort]]></category><category><![CDATA[sorting]]></category><category><![CDATA[algorithms]]></category><category><![CDATA[coding]]></category><dc:creator><![CDATA[Prateek Roy]]></dc:creator><pubDate>Sun, 20 Aug 2023 17:05:25 GMT</pubDate><content:encoded><![CDATA[<p>Bubble sort is a simple sorting algorithm that relies on comparing adjacent elements and swapping them if they are in the wrong order. In each iteration, the elements "bubble up" to their correct position and the process is repeated until the entire array is sorted.</p>
<h3 id="heading-bubble-sort-algorithm">Bubble Sort Algorithm</h3>
<ol>
<li><p>Compare the first two elements. If the first element is greater than the second element, swap them.</p>
</li>
<li><p>Move to the next pair of elements (the second and third elements), repeat the comparison and swap if necessary.</p>
</li>
<li><p>Continue this process, moving through the list one pair at a time, until you reach the end of the list. This completes one iteration. After each iteration, the largest unsorted element "bubbles up" to the end of the list.</p>
</li>
<li><p>Repeat the above steps for the number of iterations equal to the length of the list.</p>
</li>
<li><p>After all the iterations are completed, the list will be sorted.</p>
</li>
</ol>
<h3 id="heading-bubble-sort-demonstration">Bubble Sort Demonstration</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1692551035322/3b2e2686-1f9e-46a6-85a9-60614a1c08fc.gif" alt class="image--center mx-auto" /></p>
<h3 id="heading-implementation-of-bubble-sort">Implementation of Bubble Sort</h3>
<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, n</span>):</span>
    <span class="hljs-keyword">if</span> n == <span class="hljs-number">1</span>:
        <span class="hljs-keyword">return</span>

    <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">1</span>, n):
        <span class="hljs-keyword">if</span> arr[i - <span class="hljs-number">1</span>] &gt; arr[i]:
            arr[i - <span class="hljs-number">1</span>], arr[i] = arr[i], arr[i - <span class="hljs-number">1</span>]

    bubble_sort(arr, n - <span class="hljs-number">1</span>)


<span class="hljs-comment"># Example usage</span>
arr = [<span class="hljs-number">7</span>, <span class="hljs-number">0</span>, <span class="hljs-number">2</span>, <span class="hljs-number">9</span>, <span class="hljs-number">6</span>, <span class="hljs-number">1</span>, <span class="hljs-number">3</span>, <span class="hljs-number">8</span>, <span class="hljs-number">5</span>, <span class="hljs-number">4</span>]
bubble_sort(arr, len(arr))
print(<span class="hljs-string">"Sorted array:"</span>, arr)
</code></pre>
<h3 id="heading-complexity-analysis-of-bubble-sort">Complexity Analysis of Bubble Sort</h3>
<p><strong>Time Complexity:</strong> \(O(n^2)\)</p>
<p><strong>Auxiliary Space:</strong> \(O(1)\)</p>
<p>Bubble Sort has a time complexity of \(O(n^2)\) in the worst and average cases, and it's not very efficient for large lists. Other sorting algorithms like QuickSort, MergeSort, and HeapSort offer better performance for larger datasets. However, Bubble Sort's simplicity makes it a good educational example for understanding the basics of sorting algorithms.</p>
]]></content:encoded></item></channel></rss>