---
title: "折半查找法"
author: "yunlong167167"
date: "2019-06-17T18:11:06Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2019-06-17T18:11:41Z"
id: 2441920
url: "https://yunlong167167.iteye.com/blog/2441920"
---

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