Lecture 06 Ticket

Complete before the beginning of class on Wednesday, 09/14.

Download this ticket in pdf format or .tex source.

\[\def\compare{ {\mathrm{compare}} } \def\swap{ {\mathrm{swap}} } \def\sort{ {\mathrm{sort}} } \def\true{ {\mathrm{true}} } \def\false{ {\mathrm{false}} } \def\split{ {\mathrm{split}} } \def\val{ {\mathrm{val}} }\]

Suppose \(a\) is an array of numerical values of size \(n\), and \(\val\) is a numerical value. Write a method \(\split(a, \val)\) with the following behavior:

  1. \(\split\) only modifies \(a\) using the operation \(\swap(a, i, j)\) that swaps the values of \(a\) at indices \(i\) and \(j\),
  2. after calling \(\split(a, \val)\), there is an index \(k\) with \(1 \leq k \leq n\) such that for all indices \(i < k\), \(a[i] \leq \val\) and for all \(j \geq k\), \(a[j] > \val\), and
  3. \(\split\) returns the index \(k\) as in item 2.