`

折半查找法

    博客分类:
  • java
阅读更多
public static void main(String[] args) {
        int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, top, bot, mid=-1, m = 3;
        top = 0;
        bot = 9;
        while (top <= bot) {
            mid = (top + bot) / 2;
            if (a[mid] == m) {
                break;
            } else if (a[mid] < m) {
                top = mid + 1;
            } else {
                bot = mid - 1;
            }
        }

        if(top<=bot){
            System.out.println(mid+"");
        }else {
            System.out.println("...");
        }
    }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics