Given an array nums
with n
objects colored red, white, or blue, sort them in-place 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 colors red, white, and blue, respectively.
The problem can be solved efficiently by utilizing a three-pointer approach. The idea is to partition the array into three sections:
- The leftmost section contains only
0
s (red). - The middle section contains only
1
s (white). - The rightmost section contains only
2
s (blue).
By carefully swapping elements and moving pointers, we can sort the array in a single pass.
-
Initialization:
- We use three pointers:
start
,middle
, andend
. start
is the boundary for the0
s (red) section.end
is the boundary for the2
s (blue) section.middle
is used to traverse through the array.
- We use three pointers:
-
Traversing the Array:
- We start with
start = 0
,middle = 0
, andend = nums.length - 1
. - As we traverse the array using
middle
, we check the value atnums[middle]
:- If it's
0
, we swap it with the value atstart
, move bothstart
andmiddle
pointers forward. - If it's
1
, we simply move themiddle
pointer forward, as1
s should be in the middle. - If it's
2
, we swap it with the value atend
, and move theend
pointer backward.
- If it's
- We start with
-
Continue Until Done:
- We continue this process until
middle
surpassesend
. At this point, all elements are sorted with0
s at the start,1
s in the middle, and2
s at the end.
- We continue this process until
- Time Complexity: The algorithm runs in O(n) time since each element is processed at most once.
- Space Complexity: The space complexity is O(1) as we are sorting the array in place and only using a few extra variables.
class Solution {
public void sortColors(int[] nums) {
// 3-pointer Solution
int start = 0;
int middle = 0;
int end = nums.length - 1;
while (middle <= end) {
if (nums[middle] == 0) {
swap(start, middle, nums);
start++;
middle++;
} else if (nums[middle] == 1) {
middle++;
} else if (nums[middle] == 2) {
swap(middle, end, nums);
end--;
}
}
}
public void swap(int one, int two, int[] nums) {
int t = nums[one];
nums[one] = nums[two];
nums[two] = t;
}
}