简体   繁体   中英

Sorting Queue in Java with recursion

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();
    }
    }
}


    
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(); }
     */
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM