斐波那契查询方法

 

package com.cn.test.search;

import java.util.Arrays;

public class FibonacciSearch {

    private  static int maxSize = 20;

    private static int[] fib(){
        int[] f = new int[maxSize];
        f[0] = 1;
        f[1] = 1;
        for (int i = 2;if[k]){
            k++;
        }
        int [] temp = Arrays.copyOf(data,f[k]);
        //为数据补充对齐
        if(righttemp[mid]){
                left = mid +1;
                //这里减2是因为。。。。
                k -=2;
            }else {
                if(mid>right){
                    return right;
                }
                return mid;
            }
        }

        return  -1;
    }

    public static void main(String[] args) {
      int[] data = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
      int key =  15;
      int index = fibonacciSearch(data,7,0,data.length-1);
        System.out.println("data is "+ Arrays.toString(data));
        System.out.println("key = "+key+" index = "+ index);
    }
}

data is [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
key = 15 index = 6

你可能感兴趣的:(算法)