Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer) · Last updated
Notes on this paper
Pseudocode or any
high-level language (e.g. C or C++) is accepted, and the examiner's note
states explicitly that marking emphasises the operation of the program,
not syntactic details.
Reference texts for this subject.
Cormen, Leiserson, Rivest & Stein, Introduction to Algorithms,
4th ed. — tree walks (ch. 12), stacks and linear-time scans (ch. 10, 2),
asymptotic analysis (ch. 3).
Weiss, Data Structures and Algorithm Analysis in C, 2nd ed. —
arrays and dynamic 2-D allocation (ch. 1), linked lists (ch. 3), stacks
(ch. 3.3), binary trees (ch. 4).
Deitel & Deitel, C++ How to Program, 10th ed. — class
design, templates and the Rule of Three (ch. 9–12), file streams
(ch. 14).
Kernighan & Ritchie, The C Programming Language, 2nd ed.
— character/file I/O idioms (ch. 1, 7), pointers and structures
(ch. 5–6).
Stroustrup, The C++ Programming Language, 4th ed. — value
semantics, const-correctness and templates (ch. 3, 16–18).
The Computer Engineering citation list is built around architecture and
networking texts (Patterson & Hennessy, Tanenbaum, Mano); this subject is
programming and data structures, so the works above are cited instead.
Given. An array of length $n$ holding only the values 0
and 1, in the arbitrary order shown above.
Find. The same array rearranged so every 0 precedes every
1, using exactly one traversal and no sorting routine or separate counting
pass.
Approach. Maintain two pointers, left and
right, starting at the two ends of the array and moving toward
each other. Advance left past any 0 already in place, retreat
right past any 1 already in place, and when left
sits on a 1 and right sits on a 0, swap them — a
two-pointer scheme that is a two-value special case of the Dutch national
flag partition, and touches each array slot a bounded number of times.
Write the one-pass partition. The loop terminates the
moment the pointers meet or cross, which happens after at most $n$ total
pointer movements — one traversal, exactly as required, with no
separate counting pass and no comparison-based sort involved.
#include <stdio.h>
void partition_zeros_ones(int a[], int n)
{
int left = 0, right = n - 1, tmp;
while (left < right) {
if (a[left] == 0) {
left++; /* already in place, move on */
} else if (a[right] == 1) {
right--; /* already in place, move on */
} else { /* a[left]==1 and a[right]==0: swap */
tmp = a[left];
a[left] = a[right];
a[right] = tmp;
left++;
right--;
}
}
}
Trace the paper's own 15-element example. The input has
nine 0's and six 1's (counted here only to check the result, not used by the
algorithm itself, which never counts):
$$n=15,\quad \#0=9,\quad \#1=6$$
The two pointers close in from both ends, swapping only the four out-of-place
pairs, and converge with every 0 to the left of every 1:
$$\boxed{0,0,0,0,0,0,0,0,0,1,1,1,1,1,1}$$
which matches the array printed as "Array at output" in the question,
confirming the count of 9 zeros against 6 ones.