Stuck - 我自己实现的多维二进制搜索

时间:2017-02-25 17:02:43

标签: java binary-search

我有点坚持自己实现二维数组的二进制搜索。它似乎没有迭代到下一行并保持在同一列(因此是一个永无止境的循环)。二进制搜索的工作方式是从两个端点(低端点和高端点)之间的中间开始。如果查询太低,则将高端点重新调整为中间点 - 1.如果查询过高,则将低端点设置为中间端点+ 1.所有这些都发生在查询之前找到,或者没有匹配,从而导致O(log n)的最坏情况。但是,我似乎无法让数组一行一行地搜索值。以下是我到目前为止所做的事情:

public static int count(int[][]array, int query) {

        int countoccurences = 0;        
        int low = 0;                        
        int high = array[0].length - 1;
        for (int row = 0; row < array.length; row++) {
            for (int column = 0; column < array[row].length; column++) {
                while (low <= high) {

                    int mid = (low + high) / 2; //Set mid point to be (low + high) divided by 2
                    if (array[row][mid] == query ) { //Check if middle value in each column is equal to the search query
                            countoccurences++; //If it is, increment countoccurences by 1
                        } else if (array[row][mid] < query) {
                            low = mid + 1; //If it is less than query then re-adjust low to be mid index + 1
                        } else  {                           
                            high = mid - 1; //if query is too low, re-adjust high to be mid index - 1
                        } 
                } 
            }
        }   


        return countoccurences;


    }



    public static void main(String[] args) {
        int[][] array = { {7, 4, 3, 5, 10},{8, 5, 4, 6, 11},{10, 10, 8, 10, 13}, {11, 10, 15, 10, 14}};
        System.out.println("Total occurences of the number 10 is: " + count(array, 10));
    }


}

谢谢!

1 个答案:

答案 0 :(得分:0)

我认为你需要像

这样的东西
    public static int count(int[][] array, int query)
    {
        // nothing to find in an empty array
        if(array.length == 0)
            return 0;

        int countoccurences = 0;
        for (int column = 0; column < array[0].length; column++)
        {
            int low = 0;
            int high = array.length - 1;
            while (low <= high)
            {
                int mid = (low + high) / 2; //Set mid point to be (low + high) divided by 2
                if (array[mid][column] == query)
                {
                    // Check if middle value in each column is equal to the search query
                    for (int row = low; row <= high; row++)
                    {
                        if (array[row][column] == query)
                            countoccurences++; //If it is, increment countoccurences by 1
                    }
                    break;
                }
                else if (array[mid][column] < query)
                {
                    low = mid + 1; //If it is less than query then re-adjust low to be mid index + 1
                }
                else
                {
                    high = mid - 1; //if query is too low, re-adjust high to be mid index - 1
                }
            }

        }
        return countoccurences;
    }

重要变化:

  • 交换行和列以匹配您数据的实际排序方向

  • 在列中有查询的情况下引入了break;。这就是代码中无限循环的原因

  • 删除了外部for (int column = 0; column < array[row].length; column++)因为没有意义

  • 另一方面,
  • 添加内部for (int row = low; row <= high; row++)。它应该涵盖同一行中有多个目标值的情况,例如数据中的第3列。这个内循环也可以使用更有效的二进制搜索,但我太懒了。