各种查找算法

二分查找

int binary_search(int a[], int len, int key){
	int l = 0, h = len - 1;
	int m;
	while(l <= h){
		m = (l + h) / 2;
		if(a[m] == key)
			return m;
		else if(a[m] < key)
			l = m + 1;
		else
			h = m - 1;
	}
	return -1; // 未找到
}

 

猜你喜欢

转载自blog.csdn.net/ertcmmy/article/details/84032374