二分查找算法
2019-03-23 / JAVA / 863 次围观 / 0 次吐槽 /public static int getindex(int[] arr,int find) { int min = 0; int max = arr.length - 1; int mid = (min + max) / 2; while(find != arr[mid]) { if(find < arr[mid]) { max = mid -1; }else { min = mid +1; } mid = (min + max) /2; if(min > max) { return -1; } } return mid; }
核心思想:取中间的索引来进行查找
优点:平均查找性能比较好
缺点:查找的数组必须是有顺序
Powered By Cheug's Blog
Copyright Cheug Rights Reserved.