package wzs.seek; /** * 二分查找 * @author wWX154783 * */ public class Test_wzs002 { public static void main(String[] args) { int[] intArray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int result = separateSearch(intArray, 7); System.out.println("要查找的数字为下标为:" + result); } /** * 二分查找 * @param intArray 需要查找的数组 * @param number 需要查找的数字 */ static int separateSearch(int[] intArray, int number) { int left = 0; int right = intArray.length - 1; while (left <= right) { int middle = left + ((right - left) >> 1); if (number == intArray[middle]) { return middle; } else if (number < intArray[middle]) { right = middle - 1; } else { left = middle + 1; } } return -1; } }
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/10745.html