CMPS 144L Lab Activity
Red/Blue Partitioning

Complete the partition() method in the Java class PartitionTwoColor.

To ensure that the code you supply conforms to the intended loop invariant, there are assert statements to verify that the invariant is true immediately before and after each loop iteration. (If, when an assert statement is executed, the condition specified in the statement evaluates to false, an exception is thrown.)

By default, assertions are disabled in Java, which means that they have no effect. To enable them, follow these instructions.

The given Java class is an application (meaning it has a main() method), so you can run it. A sample run is shown below. It performs tests on several arrays that are "hard coded" in the program. If you want to test it on other arrays, you can enter one or more strings of R's and B's using command line arguments (or what jGrasp calls "run arguments").

$ java PartitionTwoColor

Array to be partitioned: RRBRBBRBBR
After partitioning:
RED segment:  RRRRR
BLUE segment: BBBBB

Array to be partitioned: BBBRRBBRRRBR
After partitioning:
RED segment:  RRRRRR
BLUE segment: BBBBBB

Array to be partitioned: BRBRRBRBBRB
After partitioning:
RED segment:  RRRRR
BLUE segment: BBBBBB

Array to be partitioned: RRRR
After partitioning:
RED segment:  RRRR
BLUE segment: 

Array to be partitioned: BB
After partitioning:
RED segment:  
BLUE segment: BB

Goodbye.