当数组arr={2,12,17,31,53,69,97,142,150,2...
发布于 2022-03-02 16:24:41
当数组arr= {2,12,17,31,53,69,97,142,150,240,308},value=53时,上述函数中while循环的次数为:
import java.util.Arrays public class NowCoder { public static void main(String[] args) { int[] arr= {2,12,17,31,53,69,97,142,150,240,308} System.out.println(BinarySearch(arr,53)) } public static int BinarySearch(int[] arr,int value) { int left=0 int right=arr.length-1 while(left<=right) { int mid=(left+right)/2 if(value==arr[mid]) { return mid } if(value>arr[mid]) { left=mid+1 } if(value<arr[mid]) { right=mid-1 } } return -1 } }
登录后免费查看答案
关注者
0
被浏览
2