在Java中使用递归对队列进行排序

huangapple go评论82阅读模式
英文:

Sorting Queue in Java with recursion

问题

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

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&lt;Integer&gt; queue = new LinkedList&lt;Integer&gt;();
Scanner s = new Scanner(System.in);
int n = s.nextInt();
while (n-- &gt; 0) 
queue.add(s.nextInt());
sort(queue);
}
static void FrontToLast(Queue&lt;Integer&gt; queue,int qsize) {
if (qsize &lt;= 0) {
return; 
}
queue.add(queue.peek()); 
queue.remove(); 
FrontToLast(queue, qsize - 1); 
}
static void pushInQueue(Queue&lt;Integer&gt; queue , int temp, int qsize) {
if (queue.isEmpty() || qsize == 0)  
{ 
queue.add(temp); 
return; 
}
else if (temp &lt;= queue.peek()) 
{ 
queue.add(temp);
FrontToLast(queue, qsize); 
}
else 
{ 
queue.add(queue.peek()); 
queue.remove(); 
pushInQueue(queue, temp, qsize - 1); 
}
}
static void sort(Queue&lt;Integer&gt; 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() + &quot; &quot;); 
queue.remove();
}
}
}

答案1

得分: 1

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

public class Source {
    static Queue<Integer> queue = new LinkedList<Integer>(); // 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(); }
         */
    }
}
英文:
import java.util.LinkedList;

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

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

public static void main(String args[]) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
while (n-- &gt; 0)
queue.add(s.nextInt());
sort(queue);
System.out.println(&quot;Final Result : &quot; + queue); // changed
}
static void FrontToLast(Queue&lt;Integer&gt; queue, int qsize) {
if (qsize &lt;= 0) {
return;
}
queue.add(queue.peek());
queue.remove();
FrontToLast(queue, qsize - 1);
}
static void pushInQueue(Queue&lt;Integer&gt; queue, int temp, int qsize) {
if (queue.isEmpty() || qsize == 0) {
queue.add(temp);
return;
} else if (temp &lt;= queue.peek()) {
queue.add(temp);
FrontToLast(queue, qsize);
} else {
queue.add(queue.peek());
queue.remove();
pushInQueue(queue, temp, qsize - 1);
}
}
static void sort(Queue&lt;Integer&gt; 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() + &quot; &quot;);
* queue.remove(); }
*/
}

}

huangapple
  • 本文由 发表于 2020年8月23日 11:45:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63543167.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定