This is a partition problem, not a sorting problem. We do not care about the exact order inside the even group or the odd group, only that all even numbers come first.
Problem 1: Sort Array by Parity
Problem description:
Given an integer array nums, reorder it in place so that all even numbers appear before all odd numbers. Any valid arrangement is acceptable.
What we are solving actually: The word “sort” is misleading here. We are not asked to fully order the array. We just need to partition it into two groups. That means a linear-time two-pointer partition is enough.
What we are doing actually:
- Move
leftforward until it finds an odd number in the wrong region. - Move
rightbackward until it finds an even number in the wrong region. - Swap those two misplaced values.
- Continue until the pointers cross.
class Solution {
public int[] sortArrayByParity(int[] nums) {
int left = 0;
int right = nums.length - 1;
while (left < right) {
while (left < right && nums[left] % 2 == 0) {
left++; // This value already belongs to the even prefix.
}
while (left < right && nums[right] % 2 != 0) {
right--; // This value already belongs to the odd suffix.
}
int temp = nums[left];
nums[left] = nums[right];
nums[right] = temp; // Swap the misplaced odd/even pair into correct regions.
}
return nums;
}
}
Debug steps:
- print
left,right, and the array after each swap - test
[3,1,2,4],[2,4,6], and[1,3,5] - verify the invariant that all indices
< leftare even and all indices> rightare odd
Two-Pointer Partition Idea
The array gradually splits into three regions:
- confirmed evens on the left
- unknown values in the middle
- confirmed odds on the right
left and right shrink the unknown region until nothing remains.
This is exactly the same partition style used in many quicksort-like and array-rearrangement problems.
Dry Run
Input: [3,1,2,4]
leftstops at3because it is oddrightstops at4because it is even- swap -> array becomes
[4,1,2,3]
Continue:
leftstops at1rightstops at2- swap -> array becomes
[4,2,1,3]
Pointers cross, so we stop.
Any even-first arrangement is valid, so [4,2,1,3] is a correct answer.
Why This Is Not Stable
This in-place partition does not preserve original relative order.
Example:
- input evens:
2, 4 - output may still contain
2, 4, but stability is not guaranteed in general
That is fine because the problem does not require stability. If stable order is required, extra space is usually the simpler solution.
Stable Variant
For a stable version:
- collect all evens in original order
- collect all odds in original order
- write them back
That costs O(n) extra space, but preserves the original order inside each group.
Common Mistakes
- expecting the in-place partition to be stable
- forgetting
left < rightin the inner loops - using full sorting with a comparator when partitioning is enough
- not recognizing that this is closer to partition than to sorting
Boundary Cases
- all even -> unchanged
- all odd -> unchanged
- alternating parity -> several swaps
- single element -> unchanged
Complexity
- Time:
O(n) - Space:
O(1)
Key Takeaways
- this is an in-place partition problem, not a full sorting problem
- the invariant is: even prefix on the left, odd suffix on the right
- if stable order matters, use extra space instead of forcing it into the partition version
Comments