二分查找(Binary Search):
1 int BinarySearch(int *array, int N, int key) 2 { 3 int NotFound = -1; 4 int left, right, mid; 5 left = 0; 6 right = N - 1; 7 8 while(left <= right) 9 { 10 mid = (left + right) / 2; 11 12 if (key < array[mid]) 13 right = mid - 1; 14 else if (key > array[mid]) 15 left = mid + 1; 16 else 17 return mid; 18 } 19 20 return NotFound; 21 }
二分查找判定树:
1、判定树上每个“结点”需要的查找次数刚好为该结点所在的“层数”;
2、查找成功时,“查找次数”不会超过判定树的“深度”;
3、N个结点的判定树的深度为[logN]+1;