dutch national flag problem

Initially all array values are treated as unpartitioned:
[unpartitioned values]
Array split into four parts as the iteration proceeds:
[ values < mid | values = mid | unpartitioned values | values > mid ]

procedure three-way-partition(A : array of values, mid : value):
    i ← 0
    j ← 0
    k ← size of A - 1

    while j <= k:
        if A[j] < mid:
            swap A[i] and A[j]
            i ← i + 1
            j ← j + 1
        else if A[j] > mid:
            swap A[j] and A[k]
            k ← k - 1
        else:          
            j ← j + 1


3.88
8

                                    Approach: Count the number of 0s, 1s and 2s in the given array. 
Then store all the 0s in the beginning followed by all the 1s then
all the 2s.
Algorithm:
1)Keep three counter c0 to count 0s, c1 to count 1s and c2 to count 2s
2)Traverse through the array and increase the count of c0 is the 
  element is 0,increase the count of c1 if the element is 1 and 
  increase the count of c2 if the element is 2
3)Now again traverse the array and replace first c0 elements with 0, 
  next c1 elements with 1 and next c2 elements with 2.

//GfG PseudoCode

3.88 (8 Votes)
0
4
9
Breena 105 points

                                    #include &lt;bits/stdc++.h&gt;

using namespace std;


void dutch(int arr[],int n)
{
  int low=0;
  int mid=0;
  int high=n-1;
  while(mid&lt;=high)
  {
     switch(arr[mid])
     {
     case 0:
        {
            swap(arr[low++],arr[mid++]);
            break;
        }
     case 1:
         {
             mid++;
             break;
         }
     case 2:
        {
            swap(arr[high--],arr[mid]);
            break;
        }
     }
  }
}
int main()
{
    int n;
    cout&lt;&lt;&quot;Enter the size of the array:&quot;&lt;&lt;endl;
    cin&gt;&gt;n;
    int arr[n];
    cout&lt;&lt;&quot;Enter the elements of the array:&quot;&lt;&lt;endl;
    for(int i=0;i&lt;n;i++)
    {
        cin&gt;&gt;arr[i];
    }
    dutch(arr,n);
    cout&lt;&lt;&quot;our sorted array is:&quot;&lt;&lt;endl;
    for(int i=0;i&lt;n;i++)
    {
        cout&lt;&lt;arr[i]&lt;&lt;&quot; &quot;;
    }
    cout&lt;&lt;endl;
    return 0;
}

4 (9 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
sorting array of 0 and 1 sort an array with 0,1,2 sort an array of 0's, 1's and 2's sort array of 0 1 and 2 sort an array of 0s and 1s sorting array of 0 1 and 2 Sort 0's, 1's , 2's and 3's together in an array sort a matrix of 0s and 1s sort 0s and 1s matrix dutch national flag algorithm explained sort an array of 0s 1s and 2s c++ sort array of 0,1,2 sort array of 0, 1 2 sort an array of 0s 1s and 2s codechef sorting 0,1,2 array Sort an array of 0s, 1s and 2s python solution Sort an array of 0s, 1s and 2s gfg sort an array which contains 0 1 and 2&rsquo;s Sort an array of 0's 1's &amp; 2's | sort an array of 0 1 2 sort an array of 0s 1s and 2s practice dutch national flag algorithm problem sort the array including 0 1 2 sort array of 1 and 0 sort array of 1 2 and 0 sorting array of 0s and 1s sort an array with 0 1 and 2 in O(logn) Given an array of size N containing only 0s, 1s, and 2s; sort the array in ascending order. python sort the array of 0 1 2 sort an array of 0,1,2 sort array of 0's 1's and 2's Sort an array of 0s, 1s and 2s python sort array of 0 and 1 Sort an array of 0s, and 1s Sort an array of 0s, 1s Sort an array with 0 and 1 sort an arrays of 0's 1's 2's dutch national flag problem explained Sort array with only 0s,1s,2s Sort the array of 0s, 1s, and 2s Sort an array of 0s, 1s and 2 c++ Sort an array of 0s, 1s and 2 example of dutch flag problem how to sort array of 0s 1 and 2 Sort an array of 0 and 1 sort an array of 0s 1s and 2s without sorting algo dutch flag problem sort an array of 0s 1s and 2s gfg sorting an array of 0 and 1s sorting an array of 0 1 and 2 Sort an array of 0's, 1's and 2's sorting arrays of 0 and 1 sort array with 0 and 1 sort the array of 0s 1s and 2s sort 0 1 2 in an array sort array of numbers by 1's and 0's and 2's sort array with only 0 1 and 2 sort 0s and 1s in array sorting array of 0 1 sorting array of 0 1 2 sorting an array using 0(n) sort the array of 0 and 1 sort an array of 0s and 1s and 2s in gfg sort arrays of 0 and 1 Sort array of 0s,1s and 2s sort 0,1,2 array sorting an array of 0 1 2 sort an array of 0 1 and 2 array of 0 and 1 sort sort an array with only 0,1,2 sorting array of 0 ,1 and 2 sorting array of 0 1 and 2 in o(n0 time sort 0 1 2 without sorting sort array of 0s 1s and 2s Given an array A[] consisting 1s ,2s and 3s. The task is to write a function that sorts the given array. The functions should put all 1s first, then all 2s and all 3s in last. Sort an array of 0&rsquo;s, 1&rsquo;s and 2&rsquo;s given an array consisting of 1s and 0s sort 0 1 2 using java c program to sort an array of 0's 1's and 2's Sort an array of 0's 1's &amp; 2's | Leetcod ort an array of 0&rsquo;s 1&rsquo;s 2&rsquo;s without using extra space or sorting algo sorting 0s 1s and 2s Write a program to sort an array of 0's, 1's and 2's. array consists of 0,1,2 You are given an integer array/list(ARR) of size N. It contains only 0s, 1s and 2s. Write a solution to sort this array/list in a 'single scan'. given array of 1s and 2s given an array which consist of only 1,2,0 . sort array without using any sorting algorithm sort an array of 0s 1s and 2s in c void sort012(int a[], int n) { int low=0,mid=0,high=n-1; for(mid=0;mid&lt;=high;mid++){ if(a[mid]==0){ swap(a[mid],a[low]); low++; } if(a[mid]==2){ swap(a[mid],a[high]); high--; } } } python program for dutch national flag dutch national flag problem in python Given an array A[] consisting zeroes, ones and twos, write a C program that sorts the given array that put all zeroes first, then all ones and all twos in last. python code to sort arrays containing 0,1,2 dutch flag problem solution . Sort an array of 0s, 1s and 2s You are given an integer array/list(ARR) of size N. It contains only 0s, 1s and 2s. Write a solution to sort this array/list in a 'single scan'. 'Sing Sort an array of 0s, 1s and 2s sort an array of 0s 1s sort an array containing 0, 1 and 2 in java sorting 0 1 2 sort an array of 0s 1s and 2s in javascript sort an array of 0s 1s and 2s javascript a[lo], a[mid] = a[mid], a[lo] python to js javascript solution for Sort an array of 0&rsquo;s 1&rsquo;s 2&rsquo;s without using extra space or sorting algorithm array of 0's 1's and 2's array of 0's 1's and 2 Sort an array of 0&rsquo;s 1&rsquo;s 2&rsquo;s + &quot;javascript&quot; code Sort an array of 0&rsquo;s 1&rsquo;s 2&rsquo;s without using extra space + javascript code Sort an array of 0&rsquo;s 1&rsquo;s 2&rsquo;s without using extra space or sorting algorithm + javascript Sort an array of 0&rsquo;s 1&rsquo;s 2&rsquo;s without using extra space or sorting algorithm sort 0,1,2 in array sort 0s 1s and 2s java solutioj sort 0s 1s and 2s java solution Input an unsorted array of size 9 at run time (Assume array contains only 0&rsquo;s and 1&rsquo;s). 1s and 2s and 3s in the array dutch flag algorithm sort an array 0 1 and 2 given an array which consists of only 0 1 and 2. sort the array without using any sorting algorithm Sort an array that contains only 0s, 1s and 2s. Given an array of size N containing only 0s, 1s, and 2s; sort the array in ascending order. dnf sort Sort an array of 0s, 1s and 2sSort an array of 0s, 1s and 2s sort an array of 0s, 1s and 2s without using extra space or sorting algo 2. Sort an array that contains only 0s, 1s and 2s. Given an array of size N containing 0s, 1s, and 2s; sort the array in ascending order sort an array of 0s 1s and 2s solution python sorting list one-pass algorithm using only O(1) constant space sorrt the array with number 0 1 and 2 wihout sorting algorithm sort 0,1,2 give an array 0 1 2 sort the array without using sort algorithm sort array containing 0 1 2 Sort an array of 0s, 1s and 2s solution dutch algorithm gfg sort an array 0s 1s and 2s solution cpp gfg sort an array 0s 1s and 2s solution gfg sort an array 0s 1s and 2s sorting an arrays of 0 1 2 0s 1s and 2s sorting in linear time Dutch national flag problem. Sorting Array of 0's , 1's , 2's dutch flag sorting algorithm javascript sort 0 1 2 c# arrays 0+1 = 2 Sort the array without using any sorting algo eleents are 0 1 and 2 2. Sort an array of 0&rsquo;s 1&rsquo;s 2&rsquo;s without using extra space or sorting algo give an array which consists of 0 1 and 2 dutch national flag problem given an array which consist of only 0,1, and 2 Given an array which consists of only 0, 1 and 2. Sort the array without using any sorting algo Rearrange this array having only 0,1,2 in the list such that 0 comes in the starting, after that 1 and after that 2. sort a string of 0,1,2 sort a vector of 0,1,2s in linear time cPP dutch sort when array is sorted dutch national flag problem python sort array in single pass Sort an array of 0&rsquo;s 1&rsquo;s 2&rsquo;s without using extra space or sorting algo&nbsp; sort 0 1 2 in array sort an array of 0s 1s and 2s without using counters dnf sort c++ 0 1 2 array sort sort 0 1 2 array Sort an array of 0's 1's &amp; 2's Given an array A of size N containing 0s, 1s, and 2s; you need to sort the array in ascending order. segregate 0s 1s and 2s in an array sort array of zeros ones and twos sort an array of 0s 1s and 2s without using extra space given an array of 0,1,2. sort without sorting algo sort an array with 0 1 and 2 sort array with 0 1 2 how to sort 0 1 2 in arry arraylist java Given an array A of size N containing 0s, 1s, and 2s; you need to sort the array in ascending order sort array of 0 1 2 Sort an Array containing repetitive 0,1,2 (seggregate) dutch Flag BinarySearch 24. Sort an Array containing repetitive 0,1,2 (seggregate) dutch Flag BinarySearch segregate 0 1 2 in array Given an array which consists of only 0, 1 and 2. Sort the array without using any sorting algorithm sorting 0 1 2 in an array Given an array consisting 0's, 1's and 2's, write a algorithm to sort it Sort an array of 0&rsquo;s 1&rsquo;s 2&rsquo;s sort an array of 0&rsquo;s 1&rsquo;s 2&rsquo;s without using extra space or sorting algo gfg sort an array of 0 1 2 in o(n) sort an arrayof 0s 1s and 2s Sort an array of 0s 1s 2s are given an array consisting of 0's, 1's and 2's. Your task is to sort the array. You are given an array consisting of 0's, 1's and 2's. Your task is to sort the array. An array just contains 1s, 2s, and 3s. Sort the array. sort 0-1 in o(n) c++ Sort an array of 0&rsquo;s 1&rsquo;s 2&rsquo;s without using extra space or sorting algo practice sort 0 1 2 in python sort array 0 1 2 sort 0's 1's 2's Sort an array of 0s, 1s and 2s python sort array in order 012 in place sort the colors gfg dutch national flag algorithm one pass algorithm for sorting 0 1 sort 0 1 2 Dutch national flag algorithim Sort an array of 0&rsquo;s 1&rsquo;s 2&rsquo;s without using extra space or sorting algo Given an array which consists of only 0, 1 and 2. Sort the array without using any sorting algorithm. Sort an array of 0s, 1s and 2s Given an array of size N containing 0s, 1s, and 2s; sort the array in ascending order. )Given an array which consists of only 0, 1 and 2. Sort the array without using any sorting algorithm. sort an array of 0s 1s and 2s
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source