当前位置:网站首页>Fibonacci search (golden section)
Fibonacci search (golden section)
2022-06-22 20:01:00 【Just one word】
On the basis of interpolation search , You can fix the proportional value to 0.618( The golden section ), Each time you look it up, follow (mid Number of first elements )/(mid Number of last elements )=0.618 To determine the location of the next search . In order to meet the initial state , The number of array elements conforms to a certain value in the Fibonacci sequence , You need to fill the array before you find it .
C Code :
#include <stdio.h>
#define MAXSIZE 20
void fibonacci(int *f)
{
int i;
f[0] = 1;
f[1] = 1;
for(i=2; i < MAXSIZE; ++i)
{
f[i] = f[i-2] + f[i-1];
}
}
int fibonacci_search(int *a,int key,int n)
{
int low = 0;
int high = n - 1;
int mid = 0;
int k = 0;
int F[MAXSIZE];
int i;
fibonacci(F);
while( n > F[k]-1 ) //F[k] Can be understood as the number of elements
{
++k;
}
for( i=n; i < F[k]-1; ++i)
{
a[i] = a[high];
}
while( low <= high )
{
mid = low + F[k-1] - 1;
if( a[mid] > key )
{
high = mid - 1;
k = k - 1;
}
else if( a[mid] < key )
{
low = mid + 1;
k = k - 2;
}
else
{
if( mid <= high )
{
return mid;
}
else
{
return high;
}
}
}
return -1;
}
int main()
{
int a[MAXSIZE] = {
1, 5, 15, 22, 25, 31, 39, 42, 47, 49, 59, 68, 88};
int key;
int pos;
printf(" Please enter the number you want to find :");
scanf("%d", &key);
pos = fibonacci_search(a, key, 13);
if( pos != -1 )
{
printf("\n Find success ! keyword %d The location is : %d\n\n", key, pos);
}
else
{
printf("\n Element not found in array :%d\n\n", key);
}
return 0;
}
边栏推荐
- 1.3----- simple setting of 3D slicing software
- 1.4----- PCB design? (circuit design) determination scheme
- Upgrade VS2008 crystal report to the corresponding version of vs2013
- Ts as const
- delegate
- 【深入理解TcaplusDB技术】TcaplusDB 表管理——删除表
- 【深入理解TcaplusDB技术】如何启动TcaplusDB进程
- About Random Forest
- [nfs failed to mount problem] mount nfs: access denied by server while mounting localhost:/data/dev/mysql
- vs2008 水晶报表升级到 vs2013对应版本
猜你喜欢
随机推荐
1.3-----Simplify 3D切片软件简单设置
【深入理解TcaplusDB技术】TcaplusDB运维——日常巡检
希尔排序
Definitions and terms of drawings
康考迪亚大学|图卷积循环网络用于强化学习中的奖励生成
【深入理解TcaplusDB技术】TcaplusDB事务管理——错误排查
1.2-----机械设计工具(CAD软件)和硬件设计工具(EDA软件)及对比
[deeply understand tcapulusdb technology] how to initialize and launch tcapulusdb machine
优化了一半的SQL
MySQL约束
记可视化项目代码设计的心路历程以及理解
数字货币钱包开发不知道怎么选?
[in depth understanding of tcapulusdb technology] tcapulusdb regular documents
修改隐含参数造成SQL性能下降案例之一
一文带你读懂内存泄露
Canvas picture frame
University of Calgary | recommendation system based on Reinforcement Learning
散列表(哈希表)
Ts as const
Compilation error: /usr/bin/ld: /usr/local/lib/libgflags a(gflags.cc.o): relocation R_ X86_ 64_ 32S against `. rodata‘









