<?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: viswanathan kannan</title>
    <description>The latest articles on DEV Community by viswanathan kannan (@viswanathank14).</description>
    <link>https://dev.to/viswanathank14</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%2F566948%2F04b11216-1ec2-4d8a-8757-de2080ae062c.jpeg</url>
      <title>DEV Community: viswanathan kannan</title>
      <link>https://dev.to/viswanathank14</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/viswanathank14"/>
    <language>en</language>
    <item>
      <title>Dutch National Flag Algorithm (or) 3 Way Array Partitions[Leetcode Sort Colors]</title>
      <dc:creator>viswanathan kannan</dc:creator>
      <pubDate>Fri, 24 Dec 2021 05:41:41 +0000</pubDate>
      <link>https://dev.to/viswanathank14/dutch-national-flag-algorithm-or-3-way-array-partitions-15a5</link>
      <guid>https://dev.to/viswanathank14/dutch-national-flag-algorithm-or-3-way-array-partitions-15a5</guid>
      <description>&lt;h2&gt;
  
  
  Dsa - Arrays
&lt;/h2&gt;

&lt;p&gt;Hey devs hope you all are doing well!&lt;/p&gt;

&lt;p&gt;Today we are going to learn an array based algorithm called dutch flag algorithm A.K.A 3 way parition algorithm.&lt;/p&gt;

&lt;p&gt;Before going through this article make sure you are good with array basics.&lt;/p&gt;

&lt;p&gt;Arrays basics : &lt;a href="https://www.cs.fsu.edu/%7Emyers/c++/notes/arrays.html#:%7E:text=An%20array%20is%20an%20indexed,therefore%2C%20the%20same%20size"&gt;https://www.cs.fsu.edu/~myers/c++/notes/arrays.html#:~:text=An%20array%20is%20an%20indexed,therefore%2C%20the%20same%20size&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Where is the Algorithm used ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The idea behind the algorithm is to partition an array into 3 parts using 3 pointers and can be used to sort arrays having 3 values (which can be repeated any no. of times) Ex: [0,2,1,2,0,2,2].(Sort array with 0s,1s and 2s).&lt;/p&gt;

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

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Take an unsorted array with 0s,1s and 2s.(Or any 3 numbers).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Initialize 3 variables low , mid , high with the below values :&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;low : 0&lt;/li&gt;
&lt;li&gt;mid : 0&lt;/li&gt;
&lt;li&gt;high: n-1  (Where n is the no. of array entries).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3.When the algorithm is completed elements from :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;index 0 -&amp;gt; low will have 0s.&lt;/li&gt;
&lt;li&gt;index low -&amp;gt; mid will have 1s.&lt;/li&gt;
&lt;li&gt;index mid -&amp;gt; high will have (0s/1s/2s).&lt;/li&gt;
&lt;li&gt;index high -&amp;gt; n will have 2s.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;4.There are 3 cases in this algorithm as mid moves along the array until it crosses high.&lt;/p&gt;

&lt;p&gt;5.IF list[mid] == 0 (list is our array)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Swap the values of list[mid] and list[low].&lt;/li&gt;
&lt;li&gt;Increment low and mid by one unit.&lt;/li&gt;
&lt;li&gt;We do this to ensure all 0s are put to the front.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;6.IF list[mid] == 1&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No swap is needed&lt;/li&gt;
&lt;li&gt;Increment only mid by one unit.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;7.IF list[mid] == 2&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Swap list[mid] and list[high]&lt;/li&gt;
&lt;li&gt;Increment mid and decrement high.&lt;/li&gt;
&lt;li&gt;We do this to ensure all 2s are put to the end.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;8.When mid crosses high the algorithm ends and we get a sorted array.&lt;/p&gt;

&lt;p&gt;Time Complexity : O(n)&lt;/p&gt;

&lt;p&gt;Space Complexity : O(1)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Try out yourself&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://leetcode.com/problems/sort-colors/"&gt;https://leetcode.com/problems/sort-colors/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Source Code&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution
{
public void sortColors(int[] nums)
{
int mid = 0 , high = nums.length - 1 ,low = 0;
while(mid &amp;lt;= high)
{
int value = nums[mid];

            if(value == 0)
            {
                    int swapvalue = nums[mid];
                    nums[mid] = nums[low];
                    nums[low] = swapvalue;
                    mid++;
                    low++;
            }
            else if(value == 1)
                mid++;             
            else if(value == 2)
            {
                int swap = nums[mid];
                nums[mid] = nums[high];
                nums[high] = swap;
                    high--;
            }
    }
}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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