C Program to move all even numbers to the beginning and odd numbers to the end
Learn how to move even numbers to the beginning and odd numbers to the end of a C array using an in-place two-pointer algorithm.
An array can be partitioned by parity so that every even number appears before every odd number. The values do not need to be sorted; they only need to be placed in the correct group.
For example:
Original array: 3 8 5 2 0 7 4 -6 -3
Rearranged array: -6 8 4 2 0 7 5 3 -3
All values before 7 in the result are even, and all remaining values are odd. The result may use a different order within each group because the in-place algorithm swaps misplaced values.
C Program to Place Even Numbers First and Odd Numbers Last
#include <stdio.h>
#define MAX_SIZE 100
int main(void) {
int array[MAX_SIZE];
int size;
printf("Enter the number of elements: ");
if (scanf("%d", &size) != 1 || size < 1 || size > MAX_SIZE) {
printf("Please enter a size between 1 and %d.\n", MAX_SIZE);
return 1;
}
printf("Enter %d elements:\n", size);
for (int index = 0; index < size; index++) {
if (scanf("%d", &array[index]) != 1) {
printf("Invalid array element.\n");
return 1;
}
}
int left = 0;
int right = size - 1;
while (left < right) {
while (left < right && array[left] % 2 == 0) {
left++;
}
while (left < right && array[right] % 2 != 0) {
right--;
}
if (left < right) {
int temporary = array[left];
array[left] = array[right];
array[right] = temporary;
left++;
right--;
}
}
printf("Array after grouping even and odd numbers: ");
for (int index = 0; index < size; index++) {
printf("%d ", array[index]);
}
printf("\n");
return 0;
}
Sample Output
Enter the number of elements: 9
Enter 9 elements:
3 8 5 2 0 7 4 -6 -3
Array after grouping even and odd numbers: -6 8 4 2 0 7 5 3 -3
The exact order inside the even and odd groups depends on the swaps, but every valid result has all even values before all odd values.
How the Two-Pointer Algorithm Works
The program uses two indices:
leftstarts at the beginning and looks for an odd number that is in the wrong group.rightstarts at the end and looks for an even number that is in the wrong group.
When both misplaced values are found, the program swaps them. After a swap, both indices move inward because those two positions are now correct.
while (left < right && array[left] % 2 == 0) {
left++;
}
while (left < right && array[right] % 2 != 0) {
right--;
}
The scan ends when left and right meet or cross. At that point, no odd number remains before an even number.
Dry Run
Consider this array:
3 8 5 2 0 7 4 -6 -3
The important pointer movements are:
| Step | Misplaced odd from left | Misplaced even from right | Action | Array afterward |
|---|---|---|---|---|
| 1 | 3 at index 0 | -6 at index 7 | Swap | -6 8 5 2 0 7 4 3 -3 |
| 2 | 5 at index 2 | 4 at index 6 | Swap | -6 8 4 2 0 7 5 3 -3 |
After the second swap, left advances across the even values 4, 2, and 0. The pointers then meet, so the partition is complete.
How Are Even and Odd Numbers Identified?
The remainder operator % determines parity:
value % 2 == 0
An integer is even when division by 2 leaves a remainder of zero. Otherwise, it is odd.
Zero is even because:
0 % 2 = 0
Negative values follow the same rule. For example, -6 is even and -3 is odd.
Why Use != 0 to Detect Odd Values?
The program tests an odd value with:
array[right] % 2 != 0
This works for both positive and negative integers. In C, a negative odd number can produce -1 as its remainder. Therefore, checking value % 2 == 1 can fail for negative odd values, while value % 2 != 0 is always correct.
Why Are the Boundary Checks Repeated?
Both inner loops include left < right:
while (left < right && /* parity test */)
One pointer may move several positions while searching. Rechecking the boundary prevents the pointers from crossing and avoids examining positions outside the unpartitioned section.
Is the Algorithm In Place?
Yes. Values are swapped inside the original array, and the algorithm uses only two indices and one temporary variable. It does not allocate another array, so its extra-space requirement is constant.
Does the Algorithm Preserve the Original Order?
No. This version guarantees the grouping but not the relative order within the even and odd groups. For example:
Input: 1 2 3 4
Output: 4 2 3 1
Both groups are correct, but the even values appear as 4, 2 instead of 2, 4. This is called an unstable partition.
If first-occurrence order matters, use a separate result array.
Stable Alternative Using an Additional Array
The following approach copies even values first and odd values second, preserving their original order:
int arranged[MAX_SIZE];
int writeIndex = 0;
for (int index = 0; index < size; index++) {
if (array[index] % 2 == 0) {
arranged[writeIndex] = array[index];
writeIndex++;
}
}
for (int index = 0; index < size; index++) {
if (array[index] % 2 != 0) {
arranged[writeIndex] = array[index];
writeIndex++;
}
}
for (int index = 0; index < size; index++) {
array[index] = arranged[index];
}
This stable version still takes O(n) time, but it requires O(n) extra space. The main program chooses constant extra space instead.
What If Every Number Is Even?
The left pointer advances until it reaches right. No swap is required, and the array remains unchanged:
Input: 8 0 -4 12
Output: 8 0 -4 12
What If Every Number Is Odd?
The right pointer moves toward the beginning. Again, no swap is required because there are no even values that need to move forward:
Input: 7 -3 5 1
Output: 7 -3 5 1
An all-odd array satisfies the grouping condition because the even group is empty.
What Happens with One Element?
For an array of size one, left and right are both 0. The outer loop does not execute, and the single value remains unchanged. A one-element array is already correctly partitioned whether that value is even or odd.
Grouping Is Not Sorting
Parity partitioning does not arrange values numerically:
Result: 8 2 -4 7 1 5
This result is valid even though neither group is sorted. The only requirement is that no odd value occurs before an even value. Sorting the whole array is a different operation and usually requires more work.
Time and Space Complexity
For an array of n elements:
- Time complexity:
O(n). Although the code contains nestedwhileloops, each pointer moves only inward and visits each position at most once. - Extra space complexity:
O(1). Partitioning occurs inside the original array using a fixed number of variables.
Common Mistakes
- Checking odd numbers with
value % 2 == 1, which fails for some negative odd values in C. - Treating zero as odd even though it is even.
- Moving only one pointer after a swap and checking a corrected position again.
- Omitting
left < rightfrom the inner loops. - Expecting the in-place swap method to preserve the original order within each group.
- Sorting the entire array when only parity grouping is required.
- Using nested full-array scans and increasing the running time unnecessarily.
By searching inward from both ends and swapping only misplaced values, the program groups all even numbers at the beginning and all odd numbers at the end in one linear pass with constant extra space.