Sort Numbers With a PriorityQueue
Sort Numbers With a PriorityQueue
Complete sortedOrder(int[] values) so it returns every value from smallest to largest as one string with a single space between values, letting Java's PriorityQueue do the sorting.
Reminder: Java's PriorityQueue is a ready-made min heap. You do not have to write the sift-up and sift-down code yourself: PriorityQueue<Integer> keeps its values in a heap array for you. add(x) puts a value in, peek() looks at the smallest value without removing it, and poll() removes the smallest value and returns it. Because a heap only promises that the smallest value is on top, printing the queue itself shows its internal array order, not sorted order. Taking the values out one at a time with poll() is what hands them back sorted.
The smallest(int[] values) method above it is already written and shows how to fill the queue.
Requirements:
1. Add every value to the queue, then poll until it is empty, appending each value to sb.
2. Separate values with one space and put no space at the start or the end. An empty array gives an empty string.
3. Keep duplicates: each copy comes out once.
4. Do not change smallest(int[] values).
Do NOT write a main method.
Expected Output:
3 3 7 19 25 42 1 2 5 5
= newline. The output ends with a newline — print one after the last line.
Your Output
Run your code to see the output here...