//2.顺序查找
public static int indexOf(int[] arr, int target){
int res = -1;
for (int i = 0; i < arr.length; i++){
if (arr[i] == target) {
res = i;
break;
}
}
return res;
}
//3.二分查找
public static int binarySearch(int[] arr, int target){
//1.定左右标尺
int left = 0;
int right = arr.length - 1;
//2.标尺不相错,一直循环;根据中值(算中值;和中值比较),调整标尺和返回结果
while (left <= right){
int midIndex = left + ((right - left)>>1);
int midValue = arr[midIndex];
if (target > midValue){
left = midIndex + 1;
}else if(target < midValue){
right = midIndex -1;
}else{
return midIndex;
}
}
return -1;
}