Lab: Queue Insertion Sort
Objective
In this lab you will implement and test the Queue sort secondary
method using the insertion sort algorithm. You will also familiarize
yourself with an implementation of the SortingMachine kernel using the
same implementation of insertInOrder.
Setup
To get started, import the project for this lab, QueueInsertionSort,
from the QueueInsertionSort.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 aQueue1LSort3<T>extension forQueue1L<T>, to get an idea of the overall structure and to see what themainmethod does. 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
Queue1LSort3.java, complete the body of theinsertInOrderstatic 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 insertion sort
works, open the
SortingMachine3.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. - Find the
createNewRepmethod and note how it initializes the representation fields. - Fill in the body of the private method
insertInOrderby copying and pasting your code from the previous part of the lab. It should not require any changes. - Find the "Kernel methods" section and fill in the bodies of the six kernel methods following the comments provided. Each of these methods can be implemented with a single line 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 yourSortingMachine3implementation. This requires just a few changes. 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.