<?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: Shubham Kharche</title>
    <description>The latest articles on DEV Community by Shubham Kharche (@shubham_kharche_05).</description>
    <link>https://dev.to/shubham_kharche_05</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1583471%2Ff0b6d0da-e648-418d-abf9-900a94cb1228.jpg</url>
      <title>DEV Community: Shubham Kharche</title>
      <link>https://dev.to/shubham_kharche_05</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shubham_kharche_05"/>
    <language>en</language>
    <item>
      <title>Selection Sort Algorithm</title>
      <dc:creator>Shubham Kharche</dc:creator>
      <pubDate>Wed, 18 Sep 2024 17:54:51 +0000</pubDate>
      <link>https://dev.to/shubham_kharche_05/selection-sort-algorithm-2g63</link>
      <guid>https://dev.to/shubham_kharche_05/selection-sort-algorithm-2g63</guid>
      <description>&lt;h2&gt;
  
  
  What is Selection Sort?
&lt;/h2&gt;

&lt;p&gt;The Selection Sort algorithm divides the array into two parts: the sorted part and the unsorted part. Initially, the sorted part is empty, and the unsorted part contains all the elements. The algorithm works by finding the smallest (or largest, depending on sorting order) element from the unsorted part and swapping it with the first element of the unsorted part. This process continues until the entire array is sorted.&lt;/p&gt;

&lt;h2&gt;
  
  
  Algorithm Steps
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Start with the first element in the array and assume it is the smallest.&lt;/li&gt;
&lt;li&gt;Compare this element with the other elements in the array.&lt;/li&gt;
&lt;li&gt;If you find a smaller element, swap it with the first element.&lt;/li&gt;
&lt;li&gt;Move to the next element in the array and repeat the process for the remaining unsorted elements.&lt;/li&gt;
&lt;li&gt;Continue this process until the entire array is sorted.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#Suppose we have the following array:
&lt;/span&gt;
&lt;span class="n"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;22&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Pass 1:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The smallest element between index 0 and the rest of the array is 11.&lt;/li&gt;
&lt;li&gt;We swap 64 with 11.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Array after the first pass: [11, 25, 12, 22, 64]&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Pass 2:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Now, focus on the subarray starting from index 1. The smallest element between 25, 12, 22, 64 is 12.&lt;/li&gt;
&lt;li&gt;We swap 25 with 12.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Array after the second pass: [11, 12, 25, 22, 64]&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Pass 3:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The smallest element between 25, 22, 64 is 22.&lt;/li&gt;
&lt;li&gt;We swap 25 with 22.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Array after the third pass: [11, 12, 22, 25, 64]&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Pass 4:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The subarray now contains 25, 64. Since they are already in order, no swap is needed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Final sorted array: [11, 12, 22, 25, 64]&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;selection_sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# Traverse through all array elements
&lt;/span&gt;    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
        &lt;span class="c1"&gt;# Find the minimum element in the remaining unsorted part
&lt;/span&gt;        &lt;span class="n"&gt;min_index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;min_index&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
                &lt;span class="n"&gt;min_index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;

        &lt;span class="c1"&gt;# Swap the found minimum element with the first element of the unsorted part
&lt;/span&gt;        &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;min_index&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;min_index&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;# Example usage
&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;22&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nf"&gt;selection_sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Sorted array:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Sorted array: [11, 12, 22, 25, 64]&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Time Complexity of Selection Sort:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Best Case: O(n²)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Average Case: O(n²)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Worst Case: O(n²)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Even though Selection Sort performs well for small datasets, it is not ideal for larger arrays because its time complexity is O(n²). However, it is easy to implement and can be useful in cases where memory is a concern, as Selection Sort is in-place (requires no additional memory).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Advantages and Disadvantages
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Advantages:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Simple to understand and implement.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Performs well on small lists.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Doesn't require extra memory since it sorts the array in place.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Disadvantages:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Inefficient for large datasets due to its &lt;strong&gt;O(n²)&lt;/strong&gt; time complexity.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It isn't a stable sorting algorithm, meaning equal elements might not preserve their order relative to each other.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>dsa</category>
      <category>python</category>
      <category>algorithms</category>
    </item>
  </channel>
</rss>
