package com.dk.binarysearchnorecursion;
public class BinarySearchNoRecursion {
public static void main(String[] args) {
int[] arr = {1,3,8,10,11,67,100};
int index = binarySearch(arr,1);
System.out.println("index=" + index);
}
public static int binarySearch(int[] arr,int target){
int left = 0;
int right = arr.length-1;
while (left <= right){
int mid = (left + right) / 2;
if (arr[mid] == target){
return mid;
}else if (arr[mid] > target){
right = mid - 1;
}else if (arr[mid] < target){
left = mid + 1;
}
}
return -1;
}
}
index=0
Process finished with exit code 0
转载请注明原文地址:https://ipadbbs.8miu.com/read-46791.html