Nivaar Exam Prep

25-Comp-A4 Program Design and Data Structures · December 2016

Question 8 of 9: Algorithm Design — One-Pass 0/1 Partition

Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer) · Last updated

Notes on this paper

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.

Reference texts for this subject.

  • Cormen, Leiserson, Rivest & Stein, Introduction to Algorithms, 4th ed. — tree traversals and BSTs (ch. 12), sorting (ch. 2, 7), asymptotic analysis (ch. 3).
  • Weiss, Data Structures and Algorithm Analysis in C, 2nd ed. — linked lists and queues (ch. 3), binary search trees (ch. 4).
  • Deitel & Deitel, C++ How to Program, 10th ed. — class design and templates (ch. 9–12), file streams (ch. 14), operator overloading (ch. 11).
  • Kernighan & Ritchie, The C Programming Language, 2nd ed. — character/array I/O idioms (ch. 1, 7), pointers, structures and linked lists (ch. 5–6).
  • Stroustrup, The C++ Programming Language, 4th ed. — class templates and value semantics (ch. 3, 25–27).

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 8: Algorithm Design — One-Pass 0/1 Partition (20 marks)

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.

  1. State the two-pointer invariant. Maintain 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.
  2. Write the program.
    #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;
    }
  3. Trace the paper's own 15-element example to confirm. The input 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}}$$
Question 8 — results
QuantityValue
Input array0 1 0 1 0 0 1 1 0 1 0 1 0 0 0
Output array0 0 0 0 0 0 0 0 0 1 1 1 1 1 1
Passes over the array1 (two pointers moving inward)
Time / extra space$O(n)$ time, $O(1)$ extra space