Homework: Queue III; Selection Sort
This homework is necessary preparation for the lab. Make sure you type your answers in a file you bring to the lab so that you will be able to quickly copy and paste them into the file provided in the lab.
-
Implement the static method
removeMindeclared as follows:/** * Removes and returns the minimum value from {@code q} according to the * ordering provided by the {@code compare} method from {@code order}. * * @param q * the queue * @param order * ordering by which to compare entries * @return the minimum value from {@code q} * @updates q * @requires <pre> * q /= empty_string and * [the relation computed by order.compare is a total preorder] * </pre> * @ensures <pre> * perms(q * <removeMin>, #q) and * for all x: string of character * where (x is in entries (q)) * ([relation computed by order.compare method](removeMin, x)) * </pre> */ private static String removeMin(Queue<String> q, Comparator<String> order) {...}To compare two
Strings, use theComparator<String>methodcompare(see Slides 45-50 in Queue). Do not callsortin your code forremoveMin, as this would miss the point of the problem; see below. -
Implement the static method
sortdeclared below that sorts aQueue<String>q using the "selection sort" algorithm according to the givenorder. This algorithm repeatedly callsremoveMinuntil q is empty, placing the items it returns one-by-one into a temporary queue, and then finally transferring this temporary object into q./** * Sorts {@code q} according to the ordering provided by the {@code compare} * method from {@code order}. * * @param q * the queue * @param order * ordering by which to sort * @updates q * @requires [the relation computed by order.compare is a total preorder] * @ensures q = [#q ordered by the relation computed by order.compare] */ public static void sort(Queue<String> q, Comparator<String> order) {...}