Skip to main content

Dutch National Flag Problem Vijay Sinha The Crazy Programmer

In this article, we will look into an interesting problem asked in many coding interviews related to arrays: Dutch National Flag Problem. We will discuss the problem, the intuition behind it, and the application of the algorithm in certain scenarios. Along with this we will look at different approaches to solve the problem and analyze its complexities.

Dutch National Flag Problem is among the popular programming problems suggested by E. Dijkstra. So, from the name, it is clear that the problem is related to the Dutch or National Flag of the Netherlands. The flag of the Netherlands has three colors: Red, White and Blue in the same order. So the problem is if given a set of balls belonging to these colors we have to randomly arrange balls of red, white and blue such that balls of the same color are placed together and the order is maintained. In programming, we represent this problem as : Given an Array of 0’s, 1’s and 2’s we have to arrange them such that 0’s come before 1’s and 1’s come before 2’s. So we have to sort this array and arrange them in the order similar to Dutch National Flag.

Let us understand this with an example. Consider this array:

Also, consider the 0’s, 1’s and 2’s representing the colors Red, White and Blue of the flag. We have to sort this Array to look like this:

Now, let us discuss the Dutch National Flag Algorithm to do this but first also have a look at the other approaches before getting into the algorithm to get the idea of its necessity.

  • Naïve Approach: The naïve approach will be to count the number of 0’s,1’s and 2’s in the array at first. Then we have filled the array serially with the occurrences of each number. This will require exactly 2 traversals of the array so we need to think of an approach requiring only 1 traversal.

Dutch National Flag Algorithm

Basically, the main idea of this algorithm is to move the 0’s to extreme left and 2’s to the right of the array respectively. Doing this, the 1’s automatically end up in the middle of the array. Thus, sorting the array at the end. The algorithm mainly consists of these steps:

  • We maintain three pointers Low, Mid and High. At first we set, Low and Mid at start index (0) of the array respectively and the High pointer at end index (Size – 1) of the array.
  • Then, we start iterating the array we take decisions based on Value at index Mid for each Arr[Mid] there are 3 cases.
      1. If Arr[Mid] = 0 : Then we Swap values at Arr[Low] with Arr[Mid] and increment both Low and Mid pointers.
      2. If Arr[Mid] = 1: Then we do nothing and just increment mid by 1. This ensures that all 1’s are in the middle..
      3. Or, If Arr[Mid] = 2: We Swap values at Arr[Mid] with Arr[High], then decrement the High Pointer to replace the value at high with value at mid.
  • We repeat these steps considering the given conditions until Mid <= High.

Note: This Algorithm is only useful in sorting Arrays where Three-Way Partitioning can be applied.

Implementation in Java

public class DutchNationalFlag_Sample
{
  // Swap values for case 0 and 2.
  static void swap(int arr[],int i,int j)
  {
      int temp = arr[i];
      arr[i] = arr[j];
      arr[j] = temp;
  }
    
  static void sortArray(int arr[],int n)
  {
     int mid = 0;
     int low = 0,high = n-1;
     
     while(mid <= high)
     {
         switch(arr[mid])
         {
            case 0: swap(arr,low,mid);
                    low++;                       // we increment low and mid when we get 0
                    mid++;
                    break;
                    
            case 1: mid++;                       // we increment only mid to keep 1's in the middle.
                    break;
                    
            case 2: swap(arr,mid,high); 
                    high--;                      //we decrement high as we moved 2 to last index or high.
                    break;
         }
     }
  }
    
  static void printArray(int arr[],int n)
  {
      for(int i=0;i<n;i++)
      {
          System.out.print(arr[i]+" ");
      }
      System.out.println();
  }
      
  public static void main(String args[]) 
  {
    int arr[] = { 0, 1, 2, 1, 0, 2, 1, 2, 0 }; 
        int n = arr.length; 
    
    System.out.print("Array Before Sorting: "); 
        printArray(arr,n);   
        
        //Calling sort function.
        sortArray(arr,n);
        
        System.out.print("Array After Sorting: "); 
        printArray(arr,n);   
        
  }
}

Output:

Array Before Sorting: 0 1 2 1 0 2 1 2 0 
Array After Sorting: 0 0 0 1 1 1 2 2 2

Time Complexity: We do a single traversal of the array, so the complexity will be O(n) moreover we do this in only one traversal.

Application

The Dutch National Flag Algorithm can be used in implementing Quicksort efficiently for inputs with majorly repeated elements. Quicksort exhibits its worst-case performance in such inputs taking O(n2) time. The Dutch National Flag Algorithm with its 3-Way Partitioning method can help resolve the partition routine separating the values into three groups:

  • The values less than the pivot,
  • The values equal to the pivot,
  • And, the values are greater than the pivot.

The values equal to the pivot are already sorted, so only the value less than or greater than the pivot needs to be recursively sorted. This linear-time partition routine is similar to 3–way partitioning for the Dutch national flag problem. This will make Quicksort work efficiently in O(n) time for inputs having identical values.

Let us have a quick look at the Pseudocode for the Partition routine using DNF Algorithm.

partition(arr[],start,end)
{
 mid = start;
 pivot = arr[end];
 while (mid <= end)
 {
 if (arr[mid] < pivot)
 {
 swap(arr[start], arr[mid]);
 ++start, ++mid;
 }
 else if (arr[mid] > pivot)
 {
 swap(arr[mid], arr[end]);
 --end;
 }
 else 
 {
 ++mid;
 }
 }
 // mid contains the position of partition.
 return mid;
}

So, That’s it for the article you can try out the code with different examples and execute them for a clear idea. Feel free to leave your doubts/suggestions in the comment section below.

The post Dutch National Flag Problem appeared first on The Crazy Programmer.



from The Crazy Programmer https://ift.tt/3fxTP4b

Comments

Popular posts from this blog

Difference between Web Designer and Web Developer Neeraj Mishra The Crazy Programmer

Have you ever wondered about the distinctions between web developers’ and web designers’ duties and obligations? You’re not alone! Many people have trouble distinguishing between these two. Although they collaborate to publish new websites on the internet, web developers and web designers play very different roles. To put these job possibilities into perspective, consider the construction of a house. To create a vision for the house, including the visual components, the space planning and layout, the materials, and the overall appearance and sense of the space, you need an architect. That said, to translate an idea into a building, you need construction professionals to take those architectural drawings and put them into practice. Image Source In a similar vein, web development and design work together to create websites. Let’s examine the major responsibilities and distinctions between web developers and web designers. Let’s get going, shall we? What Does a Web Designer Do?

A guide to data integration tools

CData Software is a leader in data access and connectivity solutions. It specializes in the development of data drivers and data access technologies for real-time access to online or on-premise applications, databases and web APIs. The company is focused on bringing data connectivity capabilities natively into tools organizations already use. It also features ETL/ELT solutions, enterprise connectors, and data visualization. Matillion ’s data transformation software empowers customers to extract data from a wide number of sources, load it into their chosen cloud data warehouse (CDW) and transform that data from its siloed source state, into analytics-ready insights – prepared for advanced analytics, machine learning, and artificial intelligence use cases. Only Matillion is purpose-built for Snowflake, Amazon Redshift, Google BigQuery, and Microsoft Azure, enabling businesses to achieve new levels of simplicity, speed, scale, and savings. Trusted by companies of all sizes to meet

2022: The year of hybrid work

Remote work was once considered a luxury to many, but in 2020, it became a necessity for a large portion of the workforce, as the scary and unknown COVID-19 virus sickened and even took the lives of so many people around the world.  Some workers were able to thrive in a remote setting, while others felt isolated and struggled to keep up a balance between their work and home lives. Last year saw the availability of life-saving vaccines, so companies were able to start having the conversation about what to do next. Should they keep everyone remote? Should they go back to working in the office full time? Or should they do something in between? Enter hybrid work, which offers a mix of the two. A Fall 2021 study conducted by Google revealed that over 75% of survey respondents expect hybrid work to become a standard practice within their organization within the next three years.  Thus, two years after the world abruptly shifted to widespread adoption of remote work, we are declaring 20