Java中的递归三元搜索:将数组划分为三个部分,并针对给定元素递归搜索它们

时间:2011-11-29 05:21:14

标签: java recursion ternary

我正在尝试在java数组中为给定元素实现三元搜索。此时,我在数组的下三分之一和上三分之一处得到StackOverflowError,而在中间三分之一处得到错误的结果。非常感谢。

public class TernarySearch {

    static int[] ints = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    public static int search(int[] a, int x, int low, int high) {
    // int mid = (high + low) / 2;
    int thirdile = a.length / 3;
//  System.out.println(a.length/3);
//  System.out.println(thirdile);

    if (low > high) {
        System.out.println("low is greater than high. Item not found.");
        return 0;

    } else if (x > a[(high-(thirdile - 1))]) { // upper third
        System.out.println("X is greater than mid. higher third.");
        return search(a, x, (high - thirdile), high);
    } else if (x > a[thirdile - 1] && x < (high -thirdile)) { // middle third
        System.out.println("X is in the middle third.");
        return search(a, x, thirdile + 1, (high - thirdile) - 1);

    } else if (x < a[thirdile -1 ]) { // lower third
        System.out.println("X is less than thirdile. lower third.");
        return search(a, x, low, thirdile);
    }
    else {
        System.out.println("Found it at first thirdile");
        return thirdile;
    }

}

public static void main(String[] args) {

    System.out.println(search(ints, 2, 0, ints.length - 1));
}

}

2 个答案:

答案 0 :(得分:0)

好像你没有正确地减少/增加界限。例如,当你第一次调用前三分之一时,你正在检查x是否大于位置为high-(三分之一 - 1)的值,即索引6.但是你作为低值5的索引传递,当你应该将值7传递给低点时。这可能导致从未真正隔离前三分之一的值...我想在低位和中位有一个类似的错误。

答案 1 :(得分:0)

好的,所以我知道这个问题已经过时了。但我会为未来的人们提供答案。你的代码用三分法做了一些奇怪的事情。 在你的if语句中也不是全部。你不应该缺少-1或者你不应该有-1。同样在您的递归通话中,您也不会总是将低或高更改为应该是什么。希望这段代码可以帮助人们

public class TernarySearch {  
    static int[] ints = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    public static int search(int[] a, int key, int low, int high) {
        // int mid = (high + low) / 2;
        //int thirdile = (high + low)/3
        int firstThird = ((high+1)-low)/3;
        int secondThird = ((2*((high+1)-1))/3);
        //  System.out.println(a.length/3);
        //  System.out.println(thirdile);

        if (low > high) {
            System.out.println("low is greater than high."+ key +" not found.");
            return -1;

        } else if (key > a[secondThird]) { // upper third
            System.out.println("key is greater than secondThird. its in higher third.");
            return search(a, key, secondThird+1, high);
        } else if (key > a[firstThird]) { // middle third
            System.out.println("key is in the middle third.");
            return search(a, key, firstThird+1, secondThird);
            // high is secondThird, because it could be equal to the secondThird still
            // all we know is that it wasn't higher than a[secondThird], but was higher
            //than a[firstThird]
        } else if (key < a[firstThird]) { // lower third
            System.out.println("key is less than thirdile. lower third.");
            return search(a, key, low, firstThird-1);
        }
        else {
            System.out.println("Found it at first third at index: "+firstThird);
            return firstThird;
        }

    }

    public static void main(String[] args) {

        System.out.println(search(ints, 2, 0, ints.length - 1));//print out the index number that is returned
        //if it is -1 that is printed out, 2 wasn't found, else it was found
        int myKeyIndex = search(ints, 2, 0, ints.length-1);
        if(myKeyIndex != -1)
            System.out.println(ints[myKeyIndex]+ "was found in the array!!");
        else
            System.out.println("Didn't find the key!!");
    }

}