25-Comp-A4 Program Design and Data Structures · December 2016
Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)
Paper format. 98-Comp-A4 Program Design and Data Structures, December 2016 — 3 hours, closed book, no calculator permitted. Nine questions of equal weight (20 marks each: 1, 2 and 9 split as (a) 10 + (b) 10); candidates answer any six, so a complete paper is 120 marks. Pseudocode or any high-level language is accepted, and the examiner's note states explicitly that marking emphasises the operation of the program, not syntactic details. All nine questions are answered below, because the whole set is the more useful revision resource. Answers are given in C or C++ as the question dictates; each is compilable as written (or corrected where the printed paper itself has a slip), but a clear, correctly reasoned pseudocode answer would earn the same marks.
Reference texts for this subject.
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.
Question text not reproduced: the examination questions are © Engineers and Geoscientists BC. Open the official past paper (linked at the top of this page) to read the question, then follow the worked solution below.
Given. A length-$n$ array containing only 0s and 1s.
Find. The same array rearranged so every 0 precedes every 1, using one traversal and no sort, no counting pass.
Approach. This is the two-value case of the classic Dutch-national-flag partition: two pointers start at the two ends and move inward, each index examined at most once in total, swapping only when a 0 is found stranded on the right and a 1 stranded on the left.
lo (scans forward from the left) and hi (scans
backward from the right). Everything at or before lo's last
confirmed position is already known to be 0; everything at or after
hi's is already known to be 1. When a[lo]==0 it is
already in place, so lo just advances; when a[hi]==1
it is already in place, so hi just retreats; only when
a[lo]==1 and a[hi]==0 is a swap needed —
one swap exchanges both elements into their correct halves at once.
Crucially, each of the $n$ positions is inspected a bounded number of times
before lo and hi cross, giving a single $O(n)$ pass
with no separate counting step.#include <stdio.h>
void partition01(int a[], int n)
{
int lo = 0, hi = n - 1, tmp;
while (lo < hi) {
if (a[lo] == 0) {
lo++;
} else if (a[hi] == 1) {
hi--;
} else { /* a[lo]==1 and a[hi]==0: swap into place */
tmp = a[lo]; a[lo] = a[hi]; a[hi] = tmp;
lo++; hi--;
}
}
}
int main(void)
{
int a[15] = {0,1,0,1,0,0,1,1,0,1,0,1,0,0,0};
int i;
partition01(a, 15);
for (i = 0; i < 15; i++) printf("%d ", a[i]);
printf("\n");
return 0;
}0 1 0 1 0 0 1 1 0 1 0 1 0 0 0 holds nine 0's and six
1's (confirmed by direct count, not used by the algorithm itself); running
the two-pointer sweep above moves every 1 rightward and every 0 leftward one
swap at a time until lo and hi cross, producing
nine 0's followed by six 1's — exactly the array the question prints as
"array at output."
$$\boxed{\text{partition}(\text{input}) = \underbrace{0,\ldots,0}_{9},\ \underbrace{1,\ldots,1}_{6}}$$| Quantity | Value |
|---|---|
| Input array | 0 1 0 1 0 0 1 1 0 1 0 1 0 0 0 |
| Output array | 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 |
| Passes over the array | 1 (two pointers moving inward) |
| Time / extra space | $O(n)$ time, $O(1)$ extra space |