二维数组的二分查找

在这里插入图片描述

import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        long[][] array = {
                {1, 2, 3, 4, 5},
                {6, 7, 8, 9, 10},
                {11, 12, 13, 14, 15}
        };
        int rows = 3;
        int columns = 5;

        for (long target = 0; target <= 16; target++) {
            int[] index = Search.search(array, rows, columns, target);

            System.out.printf("查找 %d 的结果是: %s\n", target, Arrays.toString(index));
        }
    }
}


public class Search {
    // new int[] { 0, 3 }
    public static int[] search(long[][] array, int rows, int columns, long target) {
        Range range = new Range(array, rows, columns);

        while (range.size() > 0) {
            long middleValue = range.getMiddleValue();
            if (target == middleValue) {
                return range.getMiddleIndex();
            } else if (target < middleValue) {
                range.discardRightPart();
            } else {
                range.discardLeftPart();
            }
        }

        // 只要返回特殊值表示没有找到即可
        return new int[] { -1, -1 };
        // return null;
    }
}

public class Range {
    private final long[][] array;
    private final int columns;

    private int lowRow;
    private int lowColumn;

    private int highRow;
    private int highColumn;

    public Range(long[][] array, int rows, int columns) {
        this.array = array;
        this.columns = columns;

        this.lowRow = 0;
        this.lowColumn = 0;

        this.highRow = rows - 1;
        this.highColumn = columns - 1;
    }

    public int size() {
        return (columns - lowColumn) + ((highRow - lowRow - 1) * columns) + (highColumn + 1);
    }

    public long getMiddleValue() {
        int[] index = getMiddleIndex();
        int row = index[0];
        int column = index[1];

        return array[row][column];
    }

    public int[] getMiddleIndex() {
        int halfSize = size() / 2;

        int middleRow = lowRow;
        int middleColumn = lowColumn;
        middleColumn += halfSize;

        // middleColumn 还不是一个合法下标
        while (middleColumn >= columns) {
            middleRow++;
            middleColumn -= columns;
        }

        return new int[] { middleRow, middleColumn };
    }

    public void discardRightPart() {
        // 让 high 往左走,不包括 middle 位置
        int[] index = getMiddleIndex();
        int row = index[0];
        int column = index[1];

        highRow = row;
        highColumn = column - 1;    // 不要中间位置
        if (highColumn < 0) {
            highRow--;
            highColumn = columns - 1;
        }
    }

    public void discardLeftPart() {
        // 让 low 往右做,不包括 middle 位置
        int[] index = getMiddleIndex();
        int row = index[0];
        int column = index[1];

        lowRow = row;
        lowColumn = column + 1;     // 不要中间位置
        if (lowColumn >= columns) {
            lowRow++;
            lowColumn = 0;
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值