Lab: Queue Selection Sort
Objective
In this lab you will use the selection sort algorithm to implement the
sort instance method for the Queue family of components and the
removeMin static helper method.
Setup
Follow these steps to set up a project for this lab.
- Create a new Eclipse project by copying
ProjectTemplate. Name the new projectQueueSort. - Open the
srcfolder of this project and then open(default package). For this lab, you will need two files. As a starting point you can use any of the Java files. Rename oneQueue1LSort1and another oneQueueSortMainand delete the other files from the project. - Follow the link to
Queue1LSort1.java, select all the code on that page and copy it to the clipboard; then open theQueue1LSort1.javafile in Eclipse and paste the code to replace the file contents. Save the file. - Follow the link to
QueueSortMain.java, select all the code on that page and copy it to the clipboard; then open theQueueSortMain.javafile in Eclipse and paste the code to replace the file contents. Save the file.
Method
- First carefully look through the file
QueueSortMain.java, which is a test driver for aQueue1LSort1extension forQueue1L<String>, 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
Queue1LSort1.java, complete the body of theremoveMinstatic method which you should have done as the homework. - Still in
Queue1LSort1.java, complete the body of thesortinstance method. Here the code is slightly different from what you wrote on the homework, becauseQueue1LSort1here is an extension (ofQueue1L<String>),sortis an instance (not static) method, and the queue being sorted is called this, not q. - Run the
QueueSortMaintest program. You can create your own input file with strings you'd like to sort, or savelines.txtto thedatafolder in your project and use it to test your code. Fix any problems you discover.
Additional Activities
- Modify
QueueSortMainso that it sorts theStrings in decreasing lexicographic order:- Create a new nested class in
QueueSortMain.javaby copying and pastingStringLT, naming itStringGT; - Edit the
comparemethod inStringGTso that it reverses the order of comparison; - Update the
mainmethod so that this new implementation ofComparator<String>is used; - Run the program to test whether the program now sorts in the reverse order from the original.
- Create a new nested class in