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

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

Sorting Queue in Java with recursion

问题

  1. import java.util.*;
  2. public class Source {
  3. public static void main(String args[]) {
  4. Queue<Integer> queue = new LinkedList<Integer>();
  5. Scanner s = new Scanner(System.in);
  6. int n = s.nextInt();
  7. while (n-- > 0)
  8. queue.add(s.nextInt());
  9. sort(queue);
  10. }
  11. static void FrontToLast(Queue<Integer> queue, int qsize) {
  12. if (qsize <= 0) {
  13. return;
  14. }
  15. queue.add(queue.peek());
  16. queue.remove();
  17. FrontToLast(queue, qsize - 1);
  18. }
  19. static void pushInQueue(Queue<Integer> queue, int temp, int qsize) {
  20. if (queue.isEmpty() || qsize == 0)
  21. {
  22. queue.add(temp);
  23. return;
  24. }
  25. else if (temp <= queue.peek())
  26. {
  27. queue.add(temp);
  28. FrontToLast(queue, qsize);
  29. }
  30. else
  31. {
  32. queue.add(queue.peek());
  33. queue.remove();
  34. pushInQueue(queue, temp, qsize - 1);
  35. }
  36. }
  37. static void sort(Queue<Integer> queue) {
  38. if (queue.isEmpty()) {
  39. return;
  40. }
  41. int temp = queue.peek();
  42. queue.remove();
  43. sort(queue);
  44. pushInQueue(queue, temp, queue.size());
  45. while (!queue.isEmpty())
  46. {
  47. System.out.print(queue.peek() + " ");
  48. queue.remove();
  49. }
  50. }
  51. }
英文:

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.

  1. import java.util.*;
  2. public class Source {
  3. public static void main(String args[]) {
  4. Queue&lt;Integer&gt; queue = new LinkedList&lt;Integer&gt;();
  5. Scanner s = new Scanner(System.in);
  6. int n = s.nextInt();
  7. while (n-- &gt; 0)
  8. queue.add(s.nextInt());
  9. sort(queue);
  10. }
  11. static void FrontToLast(Queue&lt;Integer&gt; queue,int qsize) {
  12. if (qsize &lt;= 0) {
  13. return;
  14. }
  15. queue.add(queue.peek());
  16. queue.remove();
  17. FrontToLast(queue, qsize - 1);
  18. }
  19. static void pushInQueue(Queue&lt;Integer&gt; queue , int temp, int qsize) {
  20. if (queue.isEmpty() || qsize == 0)
  21. {
  22. queue.add(temp);
  23. return;
  24. }
  25. else if (temp &lt;= queue.peek())
  26. {
  27. queue.add(temp);
  28. FrontToLast(queue, qsize);
  29. }
  30. else
  31. {
  32. queue.add(queue.peek());
  33. queue.remove();
  34. pushInQueue(queue, temp, qsize - 1);
  35. }
  36. }
  37. static void sort(Queue&lt;Integer&gt; queue) {
  38. if (queue.isEmpty()) {
  39. return;
  40. }
  41. int temp = queue.peek();
  42. queue.remove();
  43. sort(queue);
  44. pushInQueue(queue, temp, queue.size());
  45. while (!queue.isEmpty())
  46. {
  47. System.out.print(queue.peek() + &quot; &quot;);
  48. queue.remove();
  49. }
  50. }
  51. }

答案1

得分: 1

  1. import java.util.LinkedList;
  2. import java.util.Queue;
  3. import java.util.Scanner;
  4. public class Source {
  5. static Queue<Integer> queue = new LinkedList<Integer>(); // changed
  6. public static void main(String args[]) {
  7. Scanner s = new Scanner(System.in);
  8. int n = s.nextInt();
  9. while (n-- > 0)
  10. queue.add(s.nextInt());
  11. sort(queue);
  12. System.out.println("Final Result: " + queue); // changed
  13. }
  14. static void FrontToLast(Queue<Integer> queue, int qsize) {
  15. if (qsize <= 0) {
  16. return;
  17. }
  18. queue.add(queue.peek());
  19. queue.remove();
  20. FrontToLast(queue, qsize - 1);
  21. }
  22. static void pushInQueue(Queue<Integer> queue, int temp, int qsize) {
  23. if (queue.isEmpty() || qsize == 0) {
  24. queue.add(temp);
  25. return;
  26. } else if (temp <= queue.peek()) {
  27. queue.add(temp);
  28. FrontToLast(queue, qsize);
  29. } else {
  30. queue.add(queue.peek());
  31. queue.remove();
  32. pushInQueue(queue, temp, qsize - 1);
  33. }
  34. }
  35. static void sort(Queue<Integer> queue) {
  36. if (queue.isEmpty()) {
  37. return;
  38. }
  39. int temp = queue.peek();
  40. queue.remove();
  41. sort(queue);
  42. pushInQueue(queue, temp, queue.size());
  43. /*
  44. * while (!queue.isEmpty()) { System.out.print(queue.peek() + " ");
  45. * queue.remove(); }
  46. */
  47. }
  48. }
英文:
  1. import java.util.LinkedList;

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

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

  1. public static void main(String args[]) {
  2. Scanner s = new Scanner(System.in);
  3. int n = s.nextInt();
  4. while (n-- &gt; 0)
  5. queue.add(s.nextInt());
  6. sort(queue);
  7. System.out.println(&quot;Final Result : &quot; + queue); // changed
  8. }
  9. static void FrontToLast(Queue&lt;Integer&gt; queue, int qsize) {
  10. if (qsize &lt;= 0) {
  11. return;
  12. }
  13. queue.add(queue.peek());
  14. queue.remove();
  15. FrontToLast(queue, qsize - 1);
  16. }
  17. static void pushInQueue(Queue&lt;Integer&gt; queue, int temp, int qsize) {
  18. if (queue.isEmpty() || qsize == 0) {
  19. queue.add(temp);
  20. return;
  21. } else if (temp &lt;= queue.peek()) {
  22. queue.add(temp);
  23. FrontToLast(queue, qsize);
  24. } else {
  25. queue.add(queue.peek());
  26. queue.remove();
  27. pushInQueue(queue, temp, qsize - 1);
  28. }
  29. }
  30. static void sort(Queue&lt;Integer&gt; queue) {
  31. if (queue.isEmpty()) {
  32. return;
  33. }
  34. int temp = queue.peek();
  35. queue.remove();
  36. sort(queue);
  37. pushInQueue(queue, temp, queue.size());
  38. /*
  39. * while (!queue.isEmpty()) { System.out.print(queue.peek() + &quot; &quot;);
  40. * queue.remove(); }
  41. */
  42. }

}

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:

确定