当前位置:网站首页>fibonacci search
fibonacci search
2022-07-05 22:47:00 【Brother Yu...】
Introduction of Fibonacci search
Two point search is familiar to everyone , Every time 1/2 Fold check , Then someone thought , If you don't follow 1/2 If you check , Will it be more efficient in some cases ? So someone thought 0.618( The golden section ), But using this decimal directly is a little less elegant . At this time, people think of Fibonacci series .
1 1 2 3 5 8 13 21 34 55 89…… We will find that as the Fibonacci sequence increases , The ratio of the two numbers will be closer and closer 0.618, Take advantage of this feature , We can apply the golden ratio to the search technology .
The basic idea
By using the concept of golden ratio, select the search point in the sequence to find , Improve search efficiency . similarly , Fibonacci search also belongs to an ordered search algorithm .
The total length of the array is F[K]-1,mid The front length is F[K-1]-1, The back length is F[K-2]-1,mid At the golden section .
Algorithm implementation
public class Fibonacci {
public static void main(String[] args) {
int[] arr = {
1, 8, 10, 89, 1000, 1234};
System.out.println(fibSearch(arr , 99));
}
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;
}
// Fibonacci looks for ( In fact, it is the point of each search , Are close to dividing the array into 0.618 ratio , Number of elements on the left / The total number of elements About equal to 0.618)
// I want to search by ( Find the element on the left of the point / The total number of elements ) near 0.618 Get the index of the search element .
// Because Fibonacci looked , The ratio of two adjacent items is close 0.618, Then it is used in the search algorithm according to the law of Fibonacci sequence .
public static int fibSearch(int[] a, int key) {
int low = 0;
int high = a.length - 1;
// A subscript representing the Fibonacci division value
int k = 0;
int mid = 0;
int f[] = fib();
// Get the Fibonacci partition value subscript , According to the length of the lookup array , Decide how many Fibonacci values are needed
while(high > f[k] - 1) {
k++;
}
// because f[k] The value may be greater than a The length of , So we need to use Arrays class , Construct a new array , And point to a[]
// The insufficient part will be used 0 fill
int[] temp = Arrays.copyOf(a, f[k]);
// In fact, you need to use a The last number of the array is filled temp
// for example :temp = {1,8,10,89,1000,1234,0,0,0} => {1,8,10,89,1000,1234,1234,1234}
for(int i = high + 1; i < temp.length; i++) {
temp[i] = a[high];
}
// Use while To cycle through , Find our number key
while(low <= high) {
mid = low + f[k - 1] - 1;
// We should continue to look to the left of the array
if(key < temp[mid]) {
high = mid - 1;
// Why k--
// All elements = The front element + Back element f[k] = f[k - 1] + f[k - 2]
// Because there are f[k - 1] Elements , So we can continue to split f[k - 1] = f[k - 2] + f[k - 3]
// That is to say f[k - 1] Left continue to search k--, Next cycle mid = f[k - 1 - 1] - 1
k--;
} else if(key > temp[mid]) {
// All elements = The front element + Back element f[k] = f[k - 1] + f[k - 2]
// Look on the right
low = mid + 1;
k -= 2;
} else {
return Math.min(mid, high);
}
}
return -1;
}
}
边栏推荐
- Solve the problem of "no input file specified" when ThinkPHP starts
- Qtquick3d real time reflection
- Paddle Serving v0.9.0 重磅发布多机多卡分布式推理框架
- H5c3 advanced - player
- 2022软件测试工程师涨薪攻略,3年如何达到30K
- Some tutorials install the database on ubantu so as not to occupy computer memory?
- 700. Search in a Binary Search Tree. Sol
- 70. Climbing Stairs. Sol
- Technology cloud report won the special contribution award for the 10th anniversary of 2013-2022 of the "cloud Ding Award" of the global cloud computing conference
- 关于MySQL的30条优化技巧,超实用
猜你喜欢
Three "factions" in the metauniverse
Common model making instructions
从 1.5 开始搭建一个微服务框架——日志追踪 traceId
Analysis of the problem that the cookie value in PHP contains a plus sign (+) and becomes a space
Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题
90后测试员:“入职阿里,这一次,我决定不在跳槽了”
d3dx9_ How to repair 31.dll_ d3dx9_ 31. Solution to missing DLL
Distance from point to line intersection and included angle of line
Depth first DFS and breadth first BFS -- traversing adjacency tables
Lesson 1: serpentine matrix
随机推荐
Record several frequently asked questions (202207)
Tensor attribute statistics
Nacos 的安装与服务的注册
Common model making instructions
Technology cloud report won the special contribution award for the 10th anniversary of 2013-2022 of the "cloud Ding Award" of the global cloud computing conference
Starting from 1.5, build a micro Service Framework -- log tracking traceid
Post-90s tester: "after joining Ali, this time, I decided not to change jobs."
Double pointer of linked list (fast and slow pointer, sequential pointer, head and tail pointer)
如何创建线程
从 1.5 开始搭建一个微服务框架——日志追踪 traceId
FBO and RBO disappeared in webgpu
记录几个常见问题(202207)
Distance from point to line intersection and included angle of line
30 optimization skills about mysql, super practical
2022软件测试工程师涨薪攻略,3年如何达到30K
Go language learning tutorial (XV)
Three "factions" in the metauniverse
第一讲:蛇形矩阵
[Chongqing Guangdong education] National Open University autumn 2018 0088-21t Insurance Introduction reference questions
Double pointeur de liste liée (pointeur rapide et lent, pointeur séquentiel, pointeur de tête et de queue)