Lab: Queue Quicksort
Objective
In this lab you will implement and test the Queue sort secondary
method using the quicksort algorithm. You will also familiarize yourself
with an implementation of the SortingMachine kernel using the same
implementations of sort and partition.
Setup
To get started, import the project for this lab, QueueQuicksort, from
the QueueQuicksort.zip file available at this
link. If you don't remember how to do this, see
the Setup
instructions
in an earlier lab.
Method
- First carefully look through the file
QueueSortMain.java, which is a test driver for aQueue1LSort4<T>extension forQueue1L<T>, to get an idea of the overall structure and to see what themainmethod does. (Except for usingQueue1LSort4<T>instead ofQueue1LSort3<T>, this is the same main program you used in the previous lab.) In particular, note the declaration of theStringLTnested class that provides an implementation for theComparator<String>interface using the lexicographic order forStrings, and its use in themainmethod. - In
Queue1LSort4.java, complete the body of thepartitionstatic method and the body of thesortinstance method with the code you wrote for your homework. - Run the
QueueSortMaintest program. You can use thelines.txtfile in thedatafolder in your project as a test input. Fix any problems you discover. - Once you are confident that your implementation of quicksort works,
open the
SortingMachine4.javafile. This is a skeleton for an implementation ofSortingMachinekernel. Look through the file carefully and follow these steps.- In the "Private members" section, note the representation which
includes three instance variables (or fields): a
booleaninsertionModeto keep track of the machine mode (insertion or extraction), aComparator<T>machineOrderthat records the ordering used to sort, and aQueue<T>entriesto keep track of the entries in the machine. (These are the same fields that were used inSortingMachine3in the previous lab.) - Find the
createNewRepmethod and note how it initializes the representation fields. (Same asSortingMachine3.) - Fill in the body of the private methods
partitionandsortby copying and pasting your code from the previous part of the lab. Here the code forsortis slightly different from what you wrote on the homework and used in the earlier part of the lab, because thesortmethod is a static (not an instance) method, and the queue being sorted is called q, not this. Make sure you update the recursive calls to call the static methodsort, otherwise your code will compile but instead ofsortbeing recursive it will call theQueuesortsecondary method. - In preparation for filling in the bodies of the six kernel methods, contrast this representation invariant (found in the @convention tag) with the one in SortingMachine3 in Eclipse project QueueInsertionSort. (SortingMachine3's representation invariant is also available here.)
- Find the "Kernel methods" section and fill in the bodies of the six kernel methods. Each of these methods can be implemented with a single line of code except for one that will require two lines of code.
- In the "Private members" section, note the representation which
includes three instance variables (or fields): a
- Back in the
QueueSortMaintest program, update the main program so that it sorts the input from the file using yourSortingMachine4implementation. This requires just a few changes that you should have already experienced in the previous lab. If you are not sure, you should look at Slides 49-50 in SortingMachine. Run your program again and make sure the output is correct. If not, find the bug(s) and fix them.
Additional Activities
- In the
testfolder, openSortingMachineTest.javaand carefully review the code provided. In particular, note theStringLTimplementation ofComparator<String>, the variableORDER, and how it is used in the two sample test cases provided. Add test cases to test the kernel methods ofSortingMachine. Run your JUnit test fixture and fix any problems that come up. Note that if you already developed a test plan forSortingMachinefor the Additional Activities of the previous lab, you can just copy and paste theSortingMachineTest.javafile from theQueueInsertionSortproject into this lab's project.