Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用递归方法进行三元搜索_Java_Arrays_Algorithm_Recursion - Fatal编程技术网

Java 使用递归方法进行三元搜索

Java 使用递归方法进行三元搜索,java,arrays,algorithm,recursion,Java,Arrays,Algorithm,Recursion,我正在编写一个递归方法,它不是执行二进制搜索算法,而是将数组拆分为三个数组,并使用三元搜索算法。我相当肯定我的递归案例是正确的,但我的基本案例似乎有问题。如果数组包含两个或更少的值,则应使用基本情况以非递归方式检查值是否在数组中并返回索引。如果找不到该值,则返回-1 出于我无法理解的原因,不管怎样,这个方法都返回-1。不管数组的大小,或者数组是否包含值。这里是方法 public static int trinarySearch(int[] array, int x, int low, int h

我正在编写一个递归方法,它不是执行二进制搜索算法,而是将数组拆分为三个数组,并使用三元搜索算法。我相当肯定我的递归案例是正确的,但我的基本案例似乎有问题。如果数组包含两个或更少的值,则应使用基本情况以非递归方式检查值是否在数组中并返回索引。如果找不到该值,则返回-1

出于我无法理解的原因,不管怎样,这个方法都返回-1。不管数组的大小,或者数组是否包含值。这里是方法

public static int trinarySearch(int[] array, int x, int low, int high) {

    if (high - low < 3) { //BASE CASE.
        for (int i = low; i < high; i++) {
            if (array[i] == x) {
                return i;
            }
        }
        return -1;
    } else { //RECURSIVE CASE.

        int firstThird = low + (high - low) / 3;
        int secondThird = low + 2 * (high - low) / 3;

        if (x <= array[firstThird]) {
            return trinarySearch(array, x, low, firstThird - 1);
        } else if (x <= array[secondThird]) {
            return trinarySearch(array, x, firstThird + 1, secondThird - 1);
        } else { // must be (x > array[secondThird])
            return trinarySearch(array, x, secondThird + 1, high);
        }
    }
}
公共静态int-trinarySearch(int[]数组、int x、int-low、int-high){
如果(高-低<3){//基本情况。
对于(int i=低;i<高;i++){
if(数组[i]==x){
返回i;
}
}
返回-1;
}else{//递归情况。
int firstThird=低+(高-低)/3;
int secondThird=低+2*(高-低)/3;

如果(x您似乎混淆了
的逻辑。通常,您会将被检查的子阵列定义为从
(包括)开始,到
(排除)结束

您可以使用
high
inclusive(正如我从您使用
array.length-1
的示例调用中所理解的那样),但随后使用类似循环的方法

for (int i = low; i < high; i++) {
for(int i=low;i
它不访问
阵列[高]


快速解决方法是更改
我想你不明白Heuster的答案。以下是我本应该做的,在我看来,Heuster也是这么说的:

public static int trinarySearch(int[] array, int x, int low, int high) {

        if (high - low < 3) { //BASE CASE.
            for (int i = low; i < high; i++) {
                if (array[i] == x) {
                    return i;
                }
            }
            return -1;
        } else { //RECURSIVE CASE.
            int firstThird = low + (high - low) / 3;
            int secondThird = low + 2 * (high - low) / 3;

            if (x < array[firstThird]) {
                return trinarySearch(array, x, low, firstThird);
            } else if (x < array[secondThird]) {
                return trinarySearch(array, x, firstThird, secondThird);
            } else { // must be (x > array[secondThird])
                return trinarySearch(array, x, secondThird, high);
            }
        }
    }
公共静态int-trinarySearch(int[]数组、int x、int-low、int-high){
如果(高-低<3){//基本情况。
对于(int i=低;i<高;i++){
if(数组[i]==x){
返回i;
}
}
返回-1;
}else{//递归情况。
int firstThird=低+(高-低)/3;
int secondThird=低+2*(高-低)/3;
if(xarray[secondThird])
返回三元搜索(数组,x,第二个,第三个,高);
}
}
}

您刚刚错过了递归部分中的一个重要条件,即
if(x==splitingIndex)

我对您的代码做了一点更改,它可以正常工作
看到变化了吗

public static int trinarySearch(int[] array, int x, int low, int high) {

    if (high - low < 3) { 
        //BASE CASE.
        for (int i = low; i < high; i++) {
            if (array[i] == x) {
                return i;
            }
        }
        return -1;
    } else { //RECURSIVE CASE.

        int firstThird = low + (high - low) / 3;
        int secondThird = low + 2 * (high - low) / 3;

        if(x == array[firstThird])
        {
            return firstThird;
        }
        else if (x < array[firstThird]) {
            return trinarySearch(array, x, low, firstThird - 1);
        }

        if(x == array[secondThird])
        {
            return secondThird;
        }
        else if (x < array[secondThird]) {
            return trinarySearch(array, x, firstThird + 1, secondThird - 1);
        }



            return trinarySearch(array, x, secondThird + 1, high);
        }
    }
公共静态int-trinarySearch(int[]数组、int x、int-low、int-high){
如果(高-低<3){
//基本情况。
对于(int i=低;i<高;i++){
if(数组[i]==x){
返回i;
}
}
返回-1;
}else{//递归情况。
int firstThird=低+(高-低)/3;
int secondThird=低+2*(高-低)/3;
if(x==数组[firstThird])
{
返回前三名;
}
else if(x
x As@MitchWheat说,递归情况下的测试与递归调用的限制不一致,并且基本情况在
high
之前停止。重新思考。插入一些打印语句或使用调试器查看心智模型中的错误所在。它现在只适用于前三分之一的值递归案例中的其他两个案例不起作用,当搜索数组最后三分之二的值时,它仍然返回-1。