本帖最后由 罗海云 于 2013-2-24 10:41 编辑
对了,看到楼下.突然还有发现有个问题没答道...就是折半查找的数组必须是有序的.. - private static int binarySearch0(short[] a, int fromIndex, int toIndex,
- short key) {
- int low = fromIndex; <FONT color=red>//下面就让我带着你跟着程序一步一步的走. 这个是我去看的src包中的源码/ low = 0;</FONT>
- int high = toIndex - 1; // high = arr.length - 1; high = 2;
- while (low <= high) { // 0 <= 2 成立向下执行
- int mid = (low + high) >>> 1; // mid = (0+2)>>>1 mid=1
- short midVal = a[mid]; midVal = a[1] midVal = 2
- if (midVal < key) midval < 3; 成立
- low = mid + 1; // low = 1+1 low = 2;
- else if (midVal > key)
- high = mid - 1;
- else
- return mid; // key found
- }
- return -(low + 1); // key not found.<FONT color=red>returnh -(2+1); 就是这样了./..</FONT>
复制代码 呵呵,解释完毕..谢谢
|