public static void sort(ComparisonKey[] keyArr) { LinkedListPriorityQueue pq = new LinkedListPriorityQueue(); for (int i = 0; i < keyArr.length; i++) { pq.insert(keyArr[i]); } for (int i = keyArr.length - 1; i >= 0; i--) { keyArr[i] = pq.remove(); } }
public static void main(String[] argv) { LinkedListPriorityQueue priorityQueue = new LinkedListPriorityQueue(); LOG.printMessage("LinkedListPriorityQueue size = " + priorityQueue.size()); LOG.printMessage("LinkedListPriorityQueue is empty = " + priorityQueue.isEmpty()); // // add first item, // ComparisonKey expectedKey = new StringKey(NAMES_ARR[0]); priorityQueue.insert(expectedKey); ComparisonKey actualKey = priorityQueue.peek(); assert (expectedKey.compareTo(actualKey) == 0); LOG.printMessage(priorityQueue.toString()); // // add Caitlin, < Ken so will be new last item // priorityQueue.insert(new StringKey(NAMES_ARR[1])); LOG.printMessage(priorityQueue.toString()); if (priorityQueue.size() != 2) { LOG.printError("priority queue size should be 2 but it is: " + priorityQueue.size()); } LOG.printMessage("Next key should be highest priority"); // // Add Meg, should be new highest key // ComparisonKey expectedHighestKey = new StringKey(NAMES_ARR[2]); priorityQueue.insert(expectedHighestKey); LOG.printMessage(priorityQueue.toString()); ComparisonKey highestKey = priorityQueue.remove(); LOG.printMessage("Highest key is " + highestKey.toString()); if (highestKey.compareTo(expectedHighestKey) == 0) { LOG.printMessage(priorityQueue.toString()); } else { LOG.printError( "Highest key should have been " + expectedHighestKey + ", but it was " + highestKey); } // // Add Jan // priorityQueue.insert(new StringKey(NAMES_ARR[3])); LOG.printMessage(priorityQueue.toString()); // sort integers int n = 1000; ComparisonKey[] keyArr = new ComparisonKey[n]; LinkedListPriorityQueue intPQ = new LinkedListPriorityQueue(); for (int i = 0; i < n; i++) { int val = (3 * i - 13); keyArr[i] = new IntegerKey(val * val); intPQ.insert(keyArr[i]); LOG.printMessage("Added " + keyArr[i].toString()); } LinkedListPriorityQueue.sort(keyArr); LOG.printMessage("Sorted array is:"); for (int i = 0; i < n; i++) { LOG.printMessage(keyArr[i].toString()); } ComparisonKey[] nameArr = new ComparisonKey[NAMES_ARR.length]; for (int i = 0; i < nameArr.length; i++) { nameArr[i] = new StringKey(NAMES_ARR[i]); } LinkedListPriorityQueue.sort(nameArr); for (int i = 0; i < nameArr.length; i++) { LOG.printMessage(nameArr[i].toString()); } }