Search algorithm---------Fibonacci search

Fibonacci Find

Also known as the golden section method , those who are interested can understand what the golden section point is

What does the golden ratio have to do with the Fibonacci sequence?

Fibonacci sequence : 1,1,2,3,5,8,13,21,34,55 found that the ratio of two adjacent numbers is infinitely close to 0.618 of the golden section value

principle

The Fibonacci search principle is similar to the previous two, only changing the position of the middle node (mid) , mid is no longer in the middle or interpolated, but is located near the golden section point , that is, mid=low+F(k- 1)-1 (F stands for Fibonacci sequence), as shown in the figure below

[External link image transfer failed. The source site may have an anti-hotlinking mechanism. It is recommended to save the image and upload it directly (img-dDgUzGoM-1611326895299) (C:\Users\王庆华\AppData\Roaming\Typora\typora-user-images\ image-20210122180623693.png)]

Understanding of F(K-1)-1

low: the front index of the array

From the properties of the Fibonacci sequence F[k]=F[k-1]+F[K-2], we can get (F[k]-1) = (F[k-1]-1) + ( F[k-2]-1) +1. This formula explains: As long as the length of the sequence table is F[k]-1, the table can be divided into two sections with lengths F[k-1]-1 and F[k-2]-1, as shown in the figure above Show. So the middle position is mid=low+F(k-1)-1

But the sequence table length n is not necessarily equal to FK-1, so the original sequence table length n needs to be increased to F[k]-1. The value of k here only needs to make F[k]-1 exactly greater than or equal to n, and the newly added position (from n+1 to F[k]-1 position) after the length of the table is increased by the following code, All are assigned to the value of the n position.

topic

Please perform Fibonacci search {1,8, 10, 89, 1000, 1234} on an ordered array, enter a number to see if this number exists in the array, find the subscript, if not, it will prompt "No This number

Still the teacher's example

Code

import java.util.Arrays;

//斐波那契算法
//author 王
//2021年1月22日18:17:16
public class FibonacciSearch {
    
    
	public static int maxSize = 20;
	public static void main(String[] args) {
    
    
		int[] arr = {
    
    1,8, 10, 89, 1000, 1234};
		System.out.println(fibSearch(arr, 89));
	}
	
	//因为后面我们mid = low+F(K-1) -1,需要使用到斐波那契数列,因此我们需要先获取到一个斐波那契数列
	//非递归方法得到一个斐波那契数列
	public static int[] fib(){
    
    
		int[] fib = new int[maxSize];
		fib[0] = 1;
		fib[1] = 1;
		for (int i = 2; i < maxSize; i++) {
    
    
			fib[i] = fib[i -1] + fib[i-2];
		}
		return fib;
	}
	
	//编写斐波那契查找算法
	/**
	 * 使用非递归的方式编写
	 * @param a			数组
	 * @param key		需要查找的关键数字
	 * @return			返回对应的下标,没有返回-1
	 */
	public static int fibSearch(int[] a,int key){
    
    
		int low = 0;
		int high = a.length - 1;
		int k = 0;//表示斐波那契分割数值的下标
		int mid = 0;//存放我们的mid
		
		int f [] = fib();//获取到我们的斐波那契数列
		//获取到斐波那契分割数值的下标
		while(high > f[k] - 1){
    
    
			k++;
		}
		// 因为f[k]值可能大于a的长度,因此需要我们使用Arrays类,构造一个新数组,并指向a
		//不足的部分会使用0填充的
		int[] temp = Arrays.copyOf(a, f[k]);
		//实际上需要使用a数组最后的数填充temp
		//{1,8, 10, 89, 1000, 1234,0,0,0,0}=>{1,8, 10, 89, 1000, 1234,1234,1234,1234}
		for (int i = high+1; i < temp.length; i++) {
    
    
			temp[i] = a[high];
		}
		//使用while来循环处理,找到我们的数key也就是以前代码中的findValue
		while(low <= high){
    
    
			mid = low + f[k-1] -1;
			if(key < temp[mid]){
    
    
				//说明我们应该继续向数组前面部分查找
				high = mid -1;
				//为什么k--
				//1.全部元素 = 前面元素 + 后面元素
				//2.f[k] = f[k-1] + f[k-2]
				//因为前面有f[k-1]个元素,所以我们可以继续拆分f[k-1] = f[k-2] + f[k-3]
				//即在f[k-1] 的前面继续查找
				//下次循环的mid = f[k-1-1]-1
				k--;
			}else if(key > temp[mid]){
    
    
				//说明我们应该继续向数组后面部分查找
				low = mid +1;
				//1.全部元素 = 前面元素 + 后面元素
				//2.f[k] = f[k-1] + f[k-2]
				//因为后面有f[k-2]个元素,所以我们可以继续拆分f[k-1] = f[k-3] + f[k-4]
				//即在f[k-2] 的前面继续查找
				//下次循环的mid = f[k-1-2]-1
				k-=2;
			}else{
    
    
				//确定返回的下标
				if(mid <= high){
    
    
					return mid;
				}else{
    
    
					return high;
				}
			}
		}
		return -1;
		
	}
}

Guess you like

Origin blog.csdn.net/qq_22155255/article/details/113006653