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.

public static QueueX selectionSortQ(QueueX myQ) { 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(); } }

here's queueX

package hw3;

//////////////////////////////////////////////////////////////// public class QueueX { private int MAX = 10000; private int maxSize; private long[] queArray; private int front; private int rear; private int nItems;

// -------------------------------------------------------------- public QueueX() // constructor { maxSize = MAX; queArray = new long[maxSize]; front = 0; rear = -1; nItems = 0; }

public QueueX(int size) { maxSize = size; queArray = new long[maxSize]; front = 0; rear = -1; nItems = 0; }

// -------------------------------------------------------------- public void enqueue(long j) // put item at rear of queue { if (isFull()) // a fixed capacity Queue. throw new IllegalStateException("Queue is full"); if (rear == maxSize - 1) // deal with wraparound rear = -1; queArray[++rear] = j; // increment rear and enqueue nItems++; // one more item }

// -------------------------------------------------------------- public long dequeue() // take item from front of queue { if (isEmpty()) throw new IllegalStateException("Queue is empty"); long temp = queArray[front++]; // get value and incr front if (front == maxSize) // deal with wraparound front = 0; nItems--; // one less item return temp; }

// -------------------------------------------------------------- public long peekFront() // peek at front of queue { return queArray[front]; }

// -------------------------------------------------------------- public boolean isEmpty() // true if queue is empty { return (nItems == 0); }

// -------------------------------------------------------------- public boolean isFull() // true if queue is full { return (nItems == maxSize); }

// -------------------------------------------------------------- public int size() // number of items in queue { return nItems; }

public void display() { if (front

@Override public String toString() { String s = ""; if (front

theQueue.dequeue(); // dequeue 3 items theQueue.dequeue(); // (10, 20, 30) theQueue.dequeue();

theQueue.enqueue(50); // enqueue 4 more items theQueue.enqueue(60); // (wraps around) theQueue.enqueue(70); theQueue.enqueue(80); // Display the current content of the Queue without dequeuing items. theQueue.display(); System.out.println(theQueue.toString()); while (!theQueue.isEmpty()) // dequeue and display { // all items long n = theQueue.dequeue(); // (40, 50, 60, 70, 80) System.out.print(n); System.out.print(" "); } System.out.println(""); } // end main()

} // end class QueueX

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!