Question: Implement the selection sort algorithm on a Queue of long-type items. Specifically, you are given the Queue class implementation and you need to write a
Implement the selection sort algorithm on a Queue of long-type items. Specifically, you are given the Queue class implementation and you need to write a method that takes a Queue and sorts it using the selection sort idea. You should go over the Queue and use enqueue(), dequeue(), peek(),... methods to sort the items in the Queue. You must work with the queue that means you are not allowed to convert the Queue into an array (or some other data structure) and then sort the array. Also, you should write the sorting part that means you cannot call a library sort method on the Queue.
Hint: you can create another auxiliary Queue to help sorting. You do not have to implement it in-place.
public class SortQueue { public static QueueX selectionSortQ(QueueX myQ) { // your code return null; // for compilation. You need to change it. } public static void main(String args[]) { QueueX myQ= new QueueX(6); myQ.enqueue(4); myQ.enqueue(1); myQ.enqueue(5); myQ.enqueue(2); myQ.enqueue(1); myQ.enqueue(3); myQ.display(); QueueX sortedQ = selectionSortQ(myQ); sortedQ.display(); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
