二分查找法

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

 

你可能感兴趣的:(二分查找)