package Algorithm.binarysearchnorecursion;
public class BinaaySearchNoRecur {
public static void main(String[] args) {
int arr [] = {1,2,3,4,5};
int b = binarySearch(arr, 0);
System.out.println(b);
}
//二分查找的非递归实现
public static int binarySearch(int [] arr, int target){
int left = 0;
int right = arr.length - 1;
boolean flag = true;
while(left <= right){
int mid = (left + right) / 2;
if (target == arr[mid]){
return mid;
}
if (target > arr[mid]){
left = mid + 1;
}else {
right = mid - 1;
}
}
return -1;
}
}
算法 - 二分查找(非递归实现二分查找)
最新推荐文章于 2024-08-10 17:46:21 发布