Java三元搜索

时间:2015-04-20 20:14:55

标签: java search ternary

我正在尝试对字符串数组进行三元搜索。我已经完成了大部分代码,我认为我正在走上正确的轨道,但似乎除了-1之外似乎没有得到任何结果。下面是我迄今为止已经创建的代码。我知道问题出在搜索算法中。我不想使用任何内置的东西,因为我正在学习。

public static void main(String[] args) {
    //declare a string array with initial size
    String[] songs =  {"Ace", "Space", "Diamond"};
    System.out.println("\nTest binary (String):");
    System.out.println(search(songs,"Ace"));

}  

public static int search(String[] x, String target) {     

   int start=0, end=x.length;

   while (start > end) {
      int midpoint1 = start+(end - start)/3;
      int midpoint2 = start +2*(end-start)/3;
      if ( target.compareTo(x[midpoint1]) == 0 ) 
          return midpoint1;
      else if ( target.compareTo(x[midpoint2]) == 0 ) 
          return midpoint2;   
      else if ( target.compareTo(x[midpoint1]) < 0 ) 
          return end = midpoint1-1;
      else if ( target.compareTo(x[midpoint2]) > 0 ) 
          return start = midpoint2+1;
  }

   return -1;
} 

5 个答案:

答案 0 :(得分:3)

你永远不会进入循环。

int start=0, end=x.length;
while (start > end)

答案 1 :(得分:1)

你的陈述是错误的,它应该包含start&lt;结束。我建议学习大多数IDE上的调试设置,因为如果你要坚持下去,那么就可以更容易地查看变量的状态。

答案 2 :(得分:1)

尝试此更正版本,除了Thomas和user2789574识别的错误之外,您还有一个错误的递归:

public static void main(String[] args) {
    //declare a string array with initial size

    String[] songs = {"Ace", "Space", "Diamond"};

    System.out.println("\nTest binary (String):");
    System.out.println(search(songs, "Ace", 0, 3));

}

public static int search(String[] x, String target, int start, int end) {

    if (start < end) {
        int midpoint1 = start + (end - start) / 3;
        int midpoint2 = start + 2 * (end - start) / 3;
        if (target.compareTo(x[midpoint1]) == 0) {
            return midpoint1;
        } else if (target.compareTo(x[midpoint2]) == 0) {
            return midpoint2;
        } else if (x[midpoint1].compareTo(x[midpoint2]) < 0) {
            return search(x, target, midpoint1, end);
        } else {
            return search(x, target, start, midpoint2);
        }
    }

    return -1;
}

答案 3 :(得分:0)

end = x.length,对于非空字符串,将始终返回大于零的数值,并且与start = 0进行比较,它将永远不会进入循环。

答案 4 :(得分:0)

我不确定它现在是否仍然有用,但是这是用Java中的循环完成的三元搜索(假设数组已预先排序):

  public static int ternSearch(String[] x, String target) {
        
        if((target.toLowerCase()).compareTo(x[0].toLowerCase()) == 0)
        {
            return 0;
        }
        if((target.toLowerCase()).compareTo(x[x.length - 1].toLowerCase()) == 0)
        {
            return x.length-1;
        }
        int mid1 = (int) Math.ceil( x.length/3);
        if((target.toLowerCase()).compareTo(x[mid1].toLowerCase()) == 0)
        {
            return mid1;
        }
        int mid2 = (int) Math.ceil( x.length*2/3);
        if((target.toLowerCase()).compareTo(x[mid2].toLowerCase()) == 0)
        {
            return mid2;
        }
        if((target.toLowerCase()).compareTo(x[0].toLowerCase()) > 0&& (target.toLowerCase()).compareTo(x[mid1].toLowerCase()) < 0)
        {
            for (int i = 1; i < mid1; i++)
            {
                if((target.toLowerCase()).compareTo(x[i].toLowerCase()) == 0) 
                {
                    return i;
                }
            }
        }
        else if((target.toLowerCase()).compareTo(x[mid1].toLowerCase()) > 0&& (target.toLowerCase()).compareTo(x[mid2].toLowerCase()) < 0)
        {
            for (int i = mid1+1; i < mid2; i++)
            {
                if((target.toLowerCase()).compareTo(x[i].toLowerCase()) == 0) 
                {
                    return i;
                }
            }
        }
        else if((target.toLowerCase()).compareTo(x[mid2].toLowerCase()) > 0&& (target.toLowerCase()).compareTo(x[x.length-1].toLowerCase()) < 0)
        {
            for (int i = mid2+1; i < x.length-2; i++)
            {
                if((target.toLowerCase()).compareTo(x[i].toLowerCase()) == 0) 
                {
                    return i;
                }
            }
        }
        return -1;
    }