Fibonacci查找算法
package it.cast.Sort_Algorithm.Sort_Algorithm;
import java.util.Arrays;
public class FibonacciSort {
public static void main(String[] args) {
int[] array = new int[]{3,7,1,9,35,9};
int index = fibonacciSort(array, 9);
System.out.println(index);
}
private static int[] fibo(int maxSize){
int[] f = new int[maxSize];
f[0]=1;
f[1]=1;
for (int i = 2; i < f.length; i++) {
f[i]=f[i-1]+f[i-2];
}
return f;
}
private static int fibonacciSort(int[] array,int key){
int low=0;
int high=array.length-1;
int mid=0; //存放分割点位置
int[] f = fibo(array.length);//获取斐波那契数列
int k=0;
//判断k的位置
while (high>f[k]-1){
k++;
}
//扩容
int[] temp = Arrays.copyOf(array, f[k]);//扩容后的数组
for (int i = high+1; i <temp.length ; i++) {
temp[i]=array[high];
}
while(low<=high){
mid=low+f[k-1]-1;
if(temp[mid]>key){
high=mid-1;
k--;
}else if(temp[mid]<key){
low=mid+1;
k-=2;
}else{
if(mid>high){
return high;
}else{
return mid;
}
}
}
return -1;
}
}