【问题标题】:Java: Why doesn't my recursive sort method work?Java:为什么我的递归排序方法不起作用?
【发布时间】:2026-02-05 18:55:01
【问题描述】:

我在下面编写的递归排序方法在使用典型的 sout 时打印地址(“[I@7852e922”),我相信这与我在 sort() 中的返回有关。我试图使该方法无效,但这似乎不起作用,因为我需要删除返回。但是,当我最终用这个烂摊子替换它时,它会打印出未排序的数组,结果证明我的方法无效:

System.out.println(Arrays.toString(sort(array2, array2.length-1)));

请放轻松,因为我在编程/递归方面一直在苦苦挣扎,因为我的先决条件训练不足,并且在写这篇文章前 10 分钟学会了一个典型的冒泡排序。这是我的整个测试程序:

TL;DR:我的排序方法有什么问题,应该如何打印?

public static void main(String[] args) {
    int array2[] = new int[]{1, 3, 5, 2, 4};
    System.out.println(Arrays.toString(sort(array2, array2.length-1)));
}

    static public int[] sort(int[] array, int n) {
    int i = array[n];
    int j = i-1;
    int temp;
    if(n == 0) {
        return array;
    } else if(i < j) {
        temp = i;
        i = j;
        j = temp;
        return sort(array, n - 1);
    } else {
        return sort(array, n - 1);
    }
}

谢谢!

编辑:反馈后,这就是我留下的。测试完美,打印我的数组排序。我发现当看代码太久时,我倾向于将 i 与 x[i] 混淆。但是,我仍然无法避免使用 Arrays.toString() 或更改为 void sort()。如果必须,我会坚持下去。话虽如此,我感谢任何进一步的帮助。

public static void main(String[] args) {
    int array2[] = new int[]{1, 3, 5, 2, 4};
    System.out.println(Arrays.toString(sort(array2, array2.length - 1)));
}

static public int[] sort(int[] array, int lastIndex) {
    int j = lastIndex - 1;
    int temp;
    if (lastIndex == 0) {
        return array;
    } else if (array[lastIndex] < array[j]) {
        temp = array[lastIndex];
        array[lastIndex] = array[j];
        array[j] = temp;
        return sort(array, lastIndex - 1);
    } else if (array[lastIndex] > array[j]) {
        return sort(array, lastIndex - 1);
    }
    return array;
}

【问题讨论】:

  • @JoakimDanielson 注意 OP 正在使用 Arrays.toString
  • int i = array[n];int j = i-1; [...] if(i &lt; j) - 从不正确。
  • @JoakimDanielson - 请阅读整个问题。它不是在问如何打印数组,而是在问为什么它打印未排序。
  • 你的else ifelse 分支在递归中做了类似的工作。这意味着您要么不需要else if,要么实际上没有进行任何排序
  • 您没有实际排序的原因是因为您没有进行任何元素交换。您上面用于交换的代码没有对数组做任何工作

标签: java arrays sorting recursion bubble-sort


【解决方案1】:

您的代码打印出来对我来说很好,尽管您的代码是错误的。

您不需要使用返回值,尽管当您再次将它分配给变量时它没有任何区别。

查看https://www.geeksforgeeks.org/recursive-bubble-sort/ 以获取有效的递归示例。

【讨论】:

    【解决方案2】:

    这是你的答案。我在代码中有内联 cmets。希望对您有所帮助。

        private static int[] sort(int[] array, int lastIndex) {
        if(lastIndex<1) { //If array has only 1 element to be sorted then return array as is.
            return array;
        }
        //move the largest number in the array to the last index of the array;
        for(int i=0;i<array.length-1;i++) {
            if(array[i] > array[i+1]) {
                //swap
                int temp = array[i];
                array[i] = array[i+1];
                array[i+1] = temp;
            }
        }
        // now we know lastIndex element is highest, sort other elements by decrementing lastIndex
        return sort(array, lastIndex-1);
    }
    

    【讨论】:

    • 希望我能避免使用'n'。我一直对这到底是什么感到困惑。谢谢!
    • @revolverocelot1944 ,您的代码仍然无法运行,例如尝试使用 int array2[] = new int[]{1, 3, 5, 2, 4,0,25,12,56,22 ,11};它将导致 [0, 1, 3, 5, 2, 4, 11, 25, 12, 56, 22]
    【解决方案3】:

    这里有很多问题。首先,您比较索引(i 和 j 是索引,它们告诉您成员在数组中的位置)。你解决这个我的比较值是这样的:

    else if(array[j] < array[i]){
         temp = array[i];
         array[i] = array[j];
         array[j] = temp;   }
    

    其次,这样写它只会执行一次冒泡排序迭代,因此您只能确保最小值将出现在数组的开头(如果您知道冒泡排序的工作原理)。为了解决这个问题,您必须再添加一层递归调用,但这次您只调用了 n - 1 个数组元素,因为您肯定对最小值进行了排序。

    在不更改排序方法的情况下解决此问题的另一种方法是调用 for 循环并执行排序方法 n 次。

    【讨论】: