【问题标题】:Recursive Selection Sorting (Java Eclipse Neon 2)递归选择排序(Java Eclipse Neon 2)
【发布时间】:2019-03-09 09:29:37
【问题描述】:

好吧好吧。我一直在研究 Java 中的递归选择排序。我已经完成了阅读、谷歌搜索、堆栈溢出,但仍然无法弄清楚。我认为由于过于复杂,我花在它上面的时间越多,代码就越糟糕。我见过的所有示例都使用多个参数,而单个参数让我失望。

下面是递归方法和驱动。给出了前 3 个 if 语句,所以我假设是必需的。

public static void selectionSort_Rec(int[] arr)
 {
    if(arr == null) throw new NullPointerException();
    if(arr.length == 0) throw new IllegalArgumentException();
    if(arr.length == 1) return;

    int startIndex = 0;


    if ( startIndex >= arr.length - 1 )
        return;

    int minIndex = startIndex;

    for ( int index = startIndex + 1; index < arr.length; index++ )
    {
        if (arr[index] < arr[minIndex] )
            minIndex = index;
        }
    int temp = arr[startIndex];
    arr[startIndex] = arr[minIndex];
    arr[minIndex] = temp;

    startIndex++;
    selectionSort_Rec(arr);
    }

// Driver method 
public static void main(String args[])  
{ 
    int arr[] = {3, 1, 5, 2, 7, 0}; 

    // Calling function 
    selectionSort_Rec(arr);
    for(int i :arr){
        System.out.print(i);
    }
} 

【问题讨论】:

    标签: java sorting recursion selection-sort


    【解决方案1】:

    你的代码有问题。
    首先你使用startIndex 并从你的数组中找到合适的数字,然后在代码末尾增加它,这是多余的,因为当再次调用函数时,它再次使用 0。每个函数调用都有它自己的变量,因此下一次调用,函数创建新的startIndex 并再次使用为零。
    您必须将它传递给函数并在每次下一个函数调用时递增。正因为如此,你的基础检查不再是真的,它被更改为检查,直到我们到达数组的末尾。
    这行代码也是多余的,因为当我们到达这一行时,我们知道arr.lenghth() 不止一个。 (但是我们的代码逻辑发生了变化,也没有必要这样做)

    if ( startIndex >= arr.length - 1 )
        return;
    

    当达到像 1 这样的基本条件时返回更好,并且不需要抛出异常,因为当它是 1 时,我们返回而不降低。 (零或空数组的条件始终为假。您也不会从数组中删除任何内容)
    我将递归函数定义为从发送给它的起始索引对数组进行排序的函数。
    结果是这样的:

    public class GFG
    {
        public static void selectionSort_Rec(int[] arr) {
            selectionSortHelper(arr, 0);
        }
    
        static void selectionSortHelper(int[] arr, int startIndex) {
            if (startIndex >= arr.length)
                return;
    
            int minIndex = startIndex;
    
            for (int index = startIndex + 1; index < arr.length; index++)
            {
                if (arr[index] < arr[minIndex])
                    minIndex = index;
            }
            int temp = arr[startIndex];
            arr[startIndex] = arr[minIndex];
            arr[minIndex] = temp;
    
            selectionSortHelper(arr, startIndex + 1);
        }
    
        // Driver method
        public static void main(String args[]) {
            int arr[] = {3, 1, 5, 2, 7, 0};
    
            // Calling function
            selectionSort_Rec(arr);
            for (int i : arr)
            {
                System.out.print(i + " ");
            }
        }
    }
    

    我希望这是你想要的。

    【讨论】:

    • 这就是我的想法,但是我不允许在递归方法中添加另一个参数。相反,讲师正在寻找我感到困惑的辅助方法。
    • 好的。是一样的。不是添加参数,而是在方法中使用额外的参数调用另一个方法(称为辅助方法)
    • 欢迎您。如果可能,请接受其他人的答案。
    猜你喜欢
    • 2018-11-26
    • 2015-02-13
    • 2015-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 2019-05-09
    相关资源
    最近更新 更多