title: Dutch national flag algorithm with four colors
body: <p>I went through a solution for two and three colors but I am unable to get it for four colors.</p>
  
  <p>Please help.</p>
  
  <p>Will it be <code>rrbb????yyyggg</code>?
  How will we swap the green flag?</p>
  
  <p>I tried the solution below but it is not working with swapping the last yellow with green.</p>
  
  <pre><code>  public class count123 {
  
  // Java program to sort an array of 0, 1 and 2,3
  
      static void sort0123(int a[], int arr_size)
      {
          int lo = 0;
          int hi = arr_size - 1;
          int mid = 0,temp=0;
          int h2=arr_size - 1;
          while (mid &lt;= hi)
          {
              switch (a[mid])
              {
              case 0:
              {
                  temp = a[lo];
                  a[lo] = a[mid];
                  a[mid] = temp;
                  lo++;
                  mid++;
                  break;
              }
              case 1:
                  mid++;
                  break;
              case 2:
              {
                  temp = a[mid];
                  a[mid] = a[hi];
                  a[hi] = temp;
  
                  hi--;
                  h2=hi;
                  break;
              }
              case 3:{
                  temp = a[mid];
                  a[mid] = a[h2];
                  a[h2] = temp;
              //  h2--;
                  //hi=h2;
                  break;
  
              }
              }
          }
      }
  
      /* Utility function to print array arr[] */
      static void printArray(int arr[], int arr_size)
      {
          int i;
          for (i = 0; i &lt; arr_size; i++)
              System.out.print(arr[i]+" ");
              System.out.println("");
      }
  
      /*Driver function to check for above functions*/
      public static void main (String[] args)
      {
          int arr[] = {0, 1, 0,1,2,2,0,3,3,0,0,1};
          int arr_size = arr.length;
          sort0123(arr, arr_size);
          System.out.println("Array after seggregation ");
          printArray(arr, arr_size);
      }
  }
  /*This code is contributed by Devesh Agrawal*/
  </code></pre>