> For the complete documentation index, see [llms.txt](https://coding-9.gitbook.io/untitled/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://coding-9.gitbook.io/untitled/array/medium/75.-sort-colors-dutch-national-flag.md).

# 75. Sort Colors/ Dutch National Flag

## Problem Statement

\
Given an array `nums` with `n` objects colored red, white, or blue, sort them [**in-place**](https://en.wikipedia.org/wiki/In-place_algorithm) so that objects of the same color are adjacent, with the colors in the order red, white, and blue.

We will use the integers `0`, `1`, and `2` to represent the color red, white, and blue, respectively.

You must solve this problem without using the library's sort function.

&#x20;

**Example 1:**

<pre><code><strong>Input: nums = [2,0,2,1,1,0]
</strong><strong>Output: [0,0,1,1,2,2]
</strong></code></pre>

**Example 2:**

<pre><code><strong>Input: nums = [2,0,1]
</strong><strong>Output: [0,1,2]
</strong></code></pre>

&#x20;

**Constraints:**

* `n == nums.length`
* `1 <= n <= 300`
* `nums[i]` is either `0`, `1`, or `2`.

&#x20;

## Intuition

```
"New technique learned 
        Dutch National flag algorithm

Basically, 3 pointers -> 
low : 0's should be to left of it
high : 2's should be to right of it

Mid  pointer does swaps


"
```

### Links

<https://leetcode.com/problems/sort-colors/>

### Video Links

<https://www.youtube.com/watch?v=oaVa-9wmpns&list=PLgUwDviBIf0rPG3Ictpu74YWBQ1CaBkm2&index=3>

### Approach 1:

```
Dutch National Flag Algorithm
```

{% code title="C++" lineNumbers="true" %}

```cpp
class Solution {
public:
    void sortColors(vector<int>& nums) {
        int low=0;
        int mid=0;
        int high=nums.size()-1;
        
        while(mid<=high){
            if(nums[mid]==0)
                swap(nums[mid++],nums[low++]);
            else if(nums[mid]==1)
                mid++;
            else
                swap(nums[mid],nums[high--]);
        }
    }
};
```

{% endcode %}

### Approach 2:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 3:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 4:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Similar Problems

###
