Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem 3 (40 points) Implement the selection sort algorithm on a Queue of long-type items. Specifically, you are given the Queue class implementation and you

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Problem 3 (40 points) 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. The implementation of the Queue class for long data type is given in the lecture slides (the code skeleton including the implementation of the Queue class is provided below for your convenience). You should go over the Queue and use enqueue0. dequeue. peek0.. 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. import java.util.NoSuchElementException; class Queue private int maxSize; private long [ queArray; private int front; private int reari private int nItems; public Queue (int s) /7 constructor maxSizes; queArray new long [maxSize]; front-0; rear-1; nItems0; public long dequeue // take item from front of queue if (isEmpty)) throw new NoSuchElementException ("Queue is empty") long temp queArray [front++); // if (front == maxSize) // deal with get value and increment front wraparound front- 0 nItems-1I one less item return temp public void enqueue (longj) I/ put item at rear of queue if (isFull )) throw new IllegalStateException ("Queue is full") if (rearmaxSize-1 / deal with wraparound rear-1 queArray[++rear]-j; I/ increment rear and insert nItems+ // one more item public long peek) 7/ peek at front of queue if (isEmpty)) throw new NoSuchElementException ("Queue is empty") return queArray[front]; public boolean isEmpty) // true if queue is empty return (nItems-0) public boolean isFull // true if queue is full return (ni terns= maxSize); public int size) I/ number of items in queue return nItems; public void display ) System.out.println("* for (int 1-0; i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Visualizing Health And Healthcare Data Creating Clear And Compelling Visualizations To See How Youre Doing

Authors: Katherine Rowell ,Lindsay Betzendahl ,Cambria Brown

1st Edition

1119680883, 978-1119680888

More Books

Students also viewed these Databases questions