英文:
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<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
得分: 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-- > 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(); }
*/
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论