`
samsongbest
  • 浏览: 161971 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

二分查找法

 
阅读更多

aa

package com.sam.searchalgorithm;

public class Bisect {

	public static int bisect(int a[], int n, int v) {
		int i, j, m;
		i = 0;
		j = n - 1;
		while (i <= j) {
			m = (i + j) / 2;
			if (v == a[m])
				return (m);
			if (v < a[m])
				j = m - 1;
			else
				i = m + 1;
		}
		return -1;
	}

	public static void main(String[] args) {
		int a[] = { 1, 3, 5, 7, 9 };
		int v = 5;
		System.out.println(Bisect.bisect(a, 5, 3));
	}

}
 

zz

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics