0

I am trying to sort a queue (in ascending order), using the recursion method. The input is entered by the user. This one below is not giving me the correct output. For instance: 12. Elements: 6 12 3 4 5 1 7 8 10 9 11 2, result: 2 11 9 10 8 7 1 5 4 3 12 6, which is incorrect. I am trying to figure out which part is not correct. I do not want to change anything in the main method.

import java.util.*;

public class Source {
    public static void main(String args[]) {
        Queue<Integer> queue = new LinkedList<Integer>();
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        while (n-- > 0) 
            queue.add(s.nextInt());
        sort(queue);
    }
    
    static void FrontToLast(Queue<Integer> queue,int qsize) {
 
    if (qsize <= 0) {
        return; 
    }
    queue.add(queue.peek()); 
    queue.remove(); 
    FrontToLast(queue, qsize - 1); 
    }
  

    static void pushInQueue(Queue<Integer> queue , int temp, int qsize) {
    if (queue.isEmpty() || qsize == 0)  
    { 
        queue.add(temp); 
        return; 
    }
    else if (temp <= queue.peek()) 
    { 
        queue.add(temp);
        FrontToLast(queue, qsize); 
    }
    else 
    { 
        queue.add(queue.peek()); 
        queue.remove(); 
        pushInQueue(queue, temp, qsize - 1); 
    }
}
    static void sort(Queue<Integer> queue) {
        if (queue.isEmpty()) {
        return; 
        }
        int temp = queue.peek();
         queue.remove(); 
         sort(queue); 
         pushInQueue(queue, temp, queue.size());
          
        while (!queue.isEmpty())  
    { 
        System.out.print(queue.peek() + " "); 
        queue.remove();
    }
    }
}


    

1 Answer 1

1
import java.util.LinkedList;

import java.util.Queue; import java.util.Scanner;

public class Source { static Queue queue = new LinkedList(); // changed

public static void main(String args[]) {

    Scanner s = new Scanner(System.in);
    int n = s.nextInt();
    while (n-- > 0)
        queue.add(s.nextInt());
    sort(queue);
    System.out.println("Final Result : " + queue); // changed
}

static void FrontToLast(Queue<Integer> queue, int qsize) {

    if (qsize <= 0) {
        return;
    }
    queue.add(queue.peek());
    queue.remove();
    FrontToLast(queue, qsize - 1);
}

static void pushInQueue(Queue<Integer> queue, int temp, int qsize) {
    if (queue.isEmpty() || qsize == 0) {
        queue.add(temp);
        return;
    } else if (temp <= queue.peek()) {
        queue.add(temp);
        FrontToLast(queue, qsize);
    } else {
        queue.add(queue.peek());
        queue.remove();
        pushInQueue(queue, temp, qsize - 1);
    }
}

static void sort(Queue<Integer> queue) {
    if (queue.isEmpty()) {
        return;
    }
    int temp = queue.peek();
    queue.remove();
    sort(queue);
    pushInQueue(queue, temp, queue.size());

    /*
     * while (!queue.isEmpty()) { System.out.print(queue.peek() + " ");
     * queue.remove(); }
     */
}

}

Sign up to request clarification or add additional context in comments.

1 Comment

Taken the "queue" in class level and removed the printing in sort method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.