仅按列搜索第一列的二维搜索

时间:2015-10-15 17:59:19

标签: java arrays algorithm multidimensional-array binary-search

所以我对编程很陌生,我试图将二进制搜索算法应用于按列排序的二维数组。我的程序只在第一列上正确执行,所以我认为它会陷入无限循环。问题似乎是在我的while循环中的第二个else-if语句中,但我完全不知道问题可能是什么。

public static void main(String[] args) {
    // TODO Auto-generated method stub

    int[][] array = { {5, 8, 10, 6},
            {20, 10, 20, 15},
            {20, 15, 25, 20}, 
            {20, 20, 32, 25} };
    int query = 20;

    int search_status;
    search_status = count(array, query);
    System.out.println(query + " occurred " + search_status + " times");

}

public static int count(int[][]array, int query){
    int count = 0;
    int low = 0;
    int high = array.length - 1;
    for (int c = 0; c < array[0].length; c++) {
        while (low <= high) {
            int mid = low + (high - low) / 2;

            if (array[mid][c] > query) {
                high = mid - 1;
            }
            else if (array[mid][c] < query) {
                low = mid + 1;
            }
            else if (array[mid][c] == query) {
                count++;
                int up = -1;
                int down = 1;
                while ((mid + up >= 0) && (array[mid + up][c] == query)) {
                    up--;
                    count++;
                }
                while ((mid + down <= array.length - 1) && (array[mid + down][c] == query)) {
                    down++;
                    count++;
                }
                return count;
            }
        }
    }
    return - 1;
}

}

1 个答案:

答案 0 :(得分:0)

首先尝试将列推入行:

public static void main(String[] args) {
    // TODO Auto-generated method stub

    int[][] array = { {5, 8, 10, 6},
            {20, 10, 20, 15},
            {20, 15, 25, 20}, 
            {20, 20, 32, 25} };
    int query = 20;

    int search_status = 0;

    for (int c = 0; c < array.length; c++)
        search_status += count(array, query, c);

    System.out.println(query + " occurred " + search_status + " times");

}

public static int count(int[][]array, int query, int row){
    int[] column = new int[array.length];
    int count = 0;
    int low = 0;
    int high = column.length - 1;

    for (int i = 0; i < array[row].length; i++)
        column[i] = array[i][row];

    while (low <= high) {
        int mid = low + (high - low) / 2;

        if (column[mid] > query) {
            high = mid - 1;
        }
        else if (column[mid] < query) {
            low = mid + 1;
        }
        else if (column[mid] == query) {
            int mover = -1;
            int counted = 1;

            // Go all the way down
            while ((mid + mover >= 0) && (column[mid + mover] == query))
            {
                mover--;
                counted++;
            }

            mover+=counted+1;
            count+=counted;

            while ((mid + mover <= column.length - 1) && (column[mid + mover] == query))
            {
                mover++;
                count++;
            }

            return count;
        }
    }

    return 0;
}

编辑:稍微好一点的实施,以免重复计算。