ITeye博客

折半查找法

yunlong167167 · 2019-06-17 18:11 · java · 编程语言 · Markdown

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("...");
    }
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。