Your major task is to complete the partition() method of this class.
Your secondary task is to complete this class.
These classes are analogous to ones that were discussed in lecture (and to which you can find links starting at the course web page).
An annoying byproduct of having three categories rather than only two is that, after an array has been partitioned, there are two boundaries between adjacent segments (the RED-WHITE and WHITE-BLUE boundaries) rather than only one (RED-BLUE). For the partition() method to return the locations of both boundaries would require that it return some kind of composite object (e.g., an array of two int's, or an OrderedPairOfInt object, perhaps) rather than a value of type int, as was the case with the partition() method discussed in lecture. Rather than having the method return a composite object, your instructor decided to introduce two new methods by which a client could obtain this information, one of which returns the location of the RED-WHITE boundary and the other of which returns the location of the WHITE-BLUE boundary.
To support this approach, it was necessary to introduce new instance variables, the names of which are rw and wb, intended to suggest "red-white" and "white-blue", respectively.
As the comments in the partition() method indicate, the code you supply must be consistent with the loop invariant depicted by this figure:
0 rw q wb N +-------------+-------------+--------------+--------------+ b | all RED | all WHITE | ? | all BLUE | +-------------+-------------+--------------+--------------+ |
Remember that rw and wb are instance variables, so they are not declared as local variables in the partition() method. On the other hand, q is declared inside partition(), as its value is meaningful only during execution of that method. (Its name is intended to suggest that it marks the starting location of the ?-segment of the array.)
You will notice that there are two assert statements in the partition() method; their purpose is to verify that the loop invariant is true immediately before and immediately after each iteration of the loop.
Before you run the "tester" program to test your work, make sure that you enable assertions (or else they will be ignored during execution).
To enable assertions when running a program from the command line (using the java command), you need to include the -ea (or the more verbose -enableassertions) option. For example, you would enter
To enable assertions in jGrasp:
To verify that assertion testing is turned on, try running the AssertTest program. It should abort due to an AssertionError.
It should look much like the first version of the method, but it should be based upon the loop invariant depicted here:
0 rw wb q N +-------------+-------------+--------------+-----------+ b | all RED | all WHITE | all BLUE | ? | +-------------+-------------+--------------+-----------+ |
You should find that the loop body necessary to maintain this loop invariant is a little bit more complicated than that needed in your first version of partition(). Indeed, you will probably find that, during certain loop iterations, two swaps of array elements are called for rather than only one.
Of course, to test your work you should also introduce and make use of a new loopInvariant2() method to check that the loop invariant holds before and after each loop iteration.