斐波那契数列 {1,1,2,3,5,8,13,21,34,55,89}相邻两个数之比,越来越接近于黄金比例 0.618改变了mid的位置 mid = low + F(k-1) -1F(k)=F(k-1)+F(k-2) F(k)-1 = [F(k-1)-1]+[F(k-2)-1]+1递归 f(k)-1假如数列长度n不一定是F(k)-1 ,将n增加至f(k)-1即可 arr[right]
public class FibonacciSearch {public static int maxSize = 20;public static int[] fib() {int[] f = new int[maxSize];f[0] = 1;f[1] = 1;for (int i = 2; i < maxSize; i++) {f[i] = f[i - 1] + f[i - 2];}return f;}public static int fibSearch(int[] arr, int key) {int low = 0;int high = arr.length - 1;int k = 0;//斐波那契下标int mid = 0;//存放mid值int[] f = fib();while (high > f[k] - 1) {k++;}//目前为止,不足的部分是用0来填充int[] temp = Arrays.copyOf(arr, f[k]);//{1,1,2,3,5,8,13,21,34,55} ->{1,1,2,3,5,8,13,21,34,55,55,55,55}for (int i = high + 1; i < temp.length; i++) {temp[i] = arr[high];}//非递归 while (low <= high) {mid = low + f[k - 1] - 1;//核心!if (key < temp[mid]) {//小于则往左//递归传参high = mid - 1;//f[k]=f[k-1]+f[k-2]k--;} else if (key > temp[mid]) {//大于则往右low = mid + 1;//f[k]=f[k-1]+f[k-2]//f[k-2]=f[k-3]+f[k-4]k -= 2;} else {if (mid <= high) {return mid;} else {return high;}}}return -1;}}