Sort Colors
Sort an array of 0s, 1s and 2s in one pass and in place with three pointers, the Dutch national flag method.
01The problem
The array holds only 0s, 1s and 2s (red, white, blue). Sort it in place so all 0s come first, then 1s, then 2s, without using the library sort. Can you do it in one pass?
| Input | Output |
|---|---|
[2, 0, 2, 1, 1, 0] | [0, 0, 1, 1, 2, 2] |
[2, 0, 1] | [0, 1, 2] |
Constraints: 1 to 300 values, each 0, 1 or 2.
Loading the rest of the lesson…