import java.util.Arrays;
// 插值查找算法,也要求数组是有序的
public class InsertValueSearch {
public static void main(String[] args) {
int[] arr = {2, 4, 5,6, 8,9, 10, 12, 14, 17, 18, 19,20};
int index = insertValueSearch(19, arr, 0, arr.length-1);
System.out.println(index);
}
public static int insertValueSearch(int findVal, int[] arr, int left, int right){
int mid = left + (findVal-arr[left])*(right-left)/(arr[right]-arr[left]);
System.out.println("hell");
if(left > right || findVal < arr[0] || findVal > arr[right]){
return -1;
}
if(findVal > arr[mid]){
return insertValueSearch(findVal, arr, mid+1, right);
}else if(findVal < arr[mid]){
return insertValueSearch(findVal, arr, left, mid-1);
}else{
return mid;
}
}
}