Question

Sort a queue in java with generic type, below is my approach which has some issues. The porpose is to implement a method to sort the elements of the queue in ascending order, but I am not sure how to deal with generic type T instead of int or string. Need your help to solve this problem. Please post the correct code and the idea, thanks!

public static <T extends Comparable<T>> int minval(Queue<T> queue, int sort) { int min_index = -1; int size = queue.size(); T

public static <t extends Comparable<T>> void sortQueue (Queue<T> queue) { for (int i = 1; i <= queue.size(); i++) { int min_i

0 0
Add a comment Improve this question Transcribed image text
Answer #1

queue.add(queue.poll(); ** Ek <terminated> Queue Sorting (Java Application) C:\Pro Unsorted size: 7 [10, 60, 30, 20, 50, 40,

import java.util.LinkedList;
import java.util.Queue;

public class QueueSorting {
        
        // Find the minElement index from (0 to unsortedSize) fraction
        public static <T extends Comparable<T>> int maxVal(Queue<T> queue, int unsortedSize) {
                int maxIndex = 0;
                T maxVal = queue.peek();
                int size = queue.size();
                
                for(int i=0; i<size; i++) {
                        
                        // remove the element and add it back to other end
                        T x = queue.poll();
                        queue.add(x);
                        
                        if(unsortedSize > i && x.compareTo(maxVal) > 0) {
                                maxIndex = i;
                                maxVal = x;
                        }
                }

                return maxIndex;
        }

        public static <T extends Comparable<T>> void insertion(Queue<T> queue, int maxIndex, int unsortedSize) {

                int size = queue.size();
                
                T maxValue = null;
                
                for(int i=0; i<unsortedSize; i++) {
                        if(i == maxIndex) {
                                maxValue = queue.poll();
                        } else {
                                queue.add(queue.poll());
                        }
                }
                
                queue.add(maxValue);
                
                for(int i=0; i<(size - unsortedSize); i++) {
                        queue.add(queue.poll());
                }
        }

        public static <T extends Comparable<T>> void sortQueue(Queue<T> queue) {
                
                int unsortedSize = queue.size();
                
                while(unsortedSize >= 1) {
                        
                        int maxIndex = maxVal(queue, unsortedSize);
                        insertion(queue, maxIndex, unsortedSize);
                        
                        System.out.println("Unsorted size: " + unsortedSize);
                        System.out.println(queue);
                        System.out.println();
                        unsortedSize--;
                }

        }

        public static void main(String[] args) {
                Queue<Integer> q = new LinkedList<Integer>();
                q.add(10);
                q.add(60);
                q.add(30);
                q.add(20);
                q.add(70);
                q.add(50);
                q.add(40);
                
                sortQueue(q);
        }

}


Please upvote, as i have given the exact answer as asked in question. Still in case of any issues in code, let me know in comments. Thanks!

Add a comment
Know the answer?
Add Answer to:
Sort a queue in java with generic type, below is my approach which has some issues....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • private static void sort(Comparable[] a, int lo, int hi) { if (hi <= lo) return; int...

    private static void sort(Comparable[] a, int lo, int hi) { if (hi <= lo) return; int j = partition(a, lo, hi); show(a, j, lo, hi); sort(a, lo, j-1); sort(a, j+1, hi); assert issorted(a, lo, hi); } Give the code fragment above, what type of sort is this? Insertion sort Bubble sort Quicksort Mergesort

  • 4. (5 Points) The following is another merge sort top down implementation, what is the running...

    4. (5 Points) The following is another merge sort top down implementation, what is the running time and space complexity for this implementation in big-0? Briefly explain your answer. public static 〈T extends Comparable〈 T> > void sort2(T[] a) { sort2(a, , a.length - 1); @Suppresswarnings ("unchecked") private static <T extends Comparable<T>> void sort2(ΤΠ a, intlo, int hi) { if (hi (z lo) return; T[] aux- (TLD new comparable[a,length]; int mid- (10 + hi) / 2; sort2 (a, lo, mid);...

  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • This implements a static void stack_push(Stack<Integer> stack) for(int i = 0; i < 5; i++) stack.push(i);...

    This implements a static void stack_push(Stack<Integer> stack) for(int i = 0; i < 5; i++) stack.push(i); LinkedList ArrayList Queue Stack

  • What will the code shown below print to the console? public class Cascade public static void...

    What will the code shown below print to the console? public class Cascade public static void print(int arg){ for(int i=0; i<arg; i++){ System.out.print(""); } System.out.println(""); if(arg > 1){ print(arg-1); } } public static void main(String[] args) { print(4); } } E B IV AA- IES XX, SE V GT 12pt Paragraph -

  • Submissions) Part A Type and run the Array class template discussed in lecture (Templates notes). Modify...

    Submissions) Part A Type and run the Array class template discussed in lecture (Templates notes). Modify the class by adding sort member function (refer to Algorithm Analysis notes for sorting algorithms) Sample usage: a. sort(); Add the needed code in main to test your function. template <class T> 1/you can use keyword typename instead of class class Array { private: T *ptr; int size; public: Array(T arr[], int s); void print(); template <class T> Array<T>:: Array (T arr[], int s)...

  • [5 marks] Using selection sort algorithm to sort the array int array[7]-5, 6, 2, 7, 9,...

    [5 marks] Using selection sort algorithm to sort the array int array[7]-5, 6, 2, 7, 9, 4, 3). Assuming we call the following function using statement selection sort (int array, 7); Please fill the table to show the changes of the array int_array after each iteration. The first column is filled. void selection_sort (int list[], int list_size) for (int i = 0; i < list size - 1; 1++) int current min = list[1]; int current_min_index-i for (int j -...

  • Java/Queues ** Task: Write a JUnit test that shows a failure in some part of the...

    Java/Queues ** Task: Write a JUnit test that shows a failure in some part of the ADT -----ArrayQueue.java------- public class ArrayQueue {    private static final int INITIAL_CAPACITY = 2; // to permit easier testing    private Object[] contents;    private int front, rear;       /**    * Create an empty queue with an initial capacity.    */    public ArrayQueue() {        contents = new Object[INITIAL_CAPACITY];    }       /**    * Add an element to...

  • Lab Description Sort all words by comparing the length of each word. The word with the...

    Lab Description Sort all words by comparing the length of each word. The word with the smallest length would come first. If you have more than one word with the same length, that group would be sorted alphabetically Input: The data file contains a list of words. The first line in the data file is an integer that represents the number of data sets to follow Output: Output the complete list of words in order by length. Sample Data 10...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT