二分法的使用场景,只针对有序排列的各种数组或集合。
另外,在查看Arrays类的时候,发先系统其实也提供了二分搜索算法的实现, 就是static int binarySearch方法
public static int binarySearch(int[] a, int key)... public static int binarySearch(int[] a, int fromIndex, int toIndex,int key)...来看下这个参数多的方法
public static int binarySearch(int[] a, int fromIndex, int toIndex, int key) { rangeCheck(a.length, fromIndex, toIndex); return binarySearch0(a, fromIndex, toIndex, key); }在进行binarySearch之前进行了,rangeCheck是什么鬼,来看一下是代码是怎么写。
/** * Checks that {@code fromIndex} and {@code toIndex} are in * the range and throws an exception if they aren't. */ private static void rangeCheck(int arrayLength, int fromIndex, int toIndex) { if (fromIndex > toIndex) { throw new IllegalArgumentException( "fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")"); } if (fromIndex < 0) { throw new ArrayIndexOutOfBoundsException(fromIndex); } if (toIndex > arrayLength) { throw new ArrayIndexOutOfBoundsException(toIndex); } }rangeCheck原来就是对输入下标的一个检查,binarySearch0才是实现的重点。
// Like public version, but without range checks. private static int binarySearch0(int[] a, int fromIndex, int toIndex, int key) { int low = fromIndex; int high = toIndex - 1; while (low <= high) { int mid = (low + high) >>> 1; int midVal = a[mid]; if (midVal < key) low = mid + 1; else if (midVal > key) high = mid - 1; else return mid; // key found } return -(low + 1); // key not found. }简单说一下,查到,则返回相应的下标,否则返回一个负数; … 跟上边自己的其实是一样的, 不过系统的写法更高大上, 除2的时候,系统是用的更为高效的位移运算,还是很有借鉴意义的。
aikongmeng 认证博客专家 RxJava 性能优化 Android Jetpack _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ ( T | o ) ( Y | o | u | t | h | , | T | o ) ( S | i | m | p | l | e | ! ) \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/ \_/