Sort Colors (Dutch National Flag)

Given an array with n objects colored red, white or blue (represented by 0, 1, 2), sort them in-place so that objects of the same color are adjacent, in the order red, white, and blue.

Input Format

nums = array of integers 0, 1, or 2

Output Format

Modify nums so that 0s come first, then 1s, then 2s

Constraints

  • 1 <= nums.length <= 300; nums[i] is 0, 1, or 2

Examples

Example 1:

Input:

nums = [2,0,2,1,1,0]

Output:

[0,0,1,1,2,2]

Explanation:

The sorted order is [0,0,1,1,2,2].

Loading...
Sort Colors (Dutch National Flag) - Arrays