当前位置:网站首页>Interpolation lookup and half (bisection) lookup
Interpolation lookup and half (bisection) lookup
2022-06-22 20:01:00 【Just one word】
The interpolation to find
#include <stdio.h>
int bin_search( int str[], int n, int key )
{
int low, high, mid;
low = 0;
high = n-1;
while( low <= high )
{
mid = low + (key-a[low]/a[high]-a[low])*(high-low); // The only difference in interpolation lookup
if( str[mid] == key )
{
return mid;
}
if( str[mid] < key )
{
low = mid + 1;
}
if( str[mid] > key )
{
high = mid - 1;
}
}
return -1;
}
int main()
{
int str[11] = {
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89};
int n, addr;
printf(" Please enter the keyword to be found : ");
scanf("%d", &n);
addr = bin_search(str, 11, n);
if( -1 != addr )
{
printf(" Find success , Gratifying congratulations , Coca Cola ! keyword %d The location is : %d\n", n, addr);
}
else
{
printf(" To find the failure !\n");
}
return 0;
}
Half ( Two points ) lookup :
#include<stdio.h>
int BinSearch(int arr[],int len,int key) // Half search ( Dichotomy )
{
int low=0; // Define the initial minimum
int high=len-1; // Define the initial maximum
int mid; // Define intermediate values
while(low<=high)
{
mid=(low+high)/2; // Find the middle value
if(key==arr[mid]) // Judge min And key Whether it is equal or not
return mid;
else if(key>arr[mid]) // If key>mid Then the new area is [mid+1,high]
low=mid+1;
else // If key<mid Then the new area is [low,mid-1]
high=mid-1;
}
return -1; // If there is no target value in the array key, Then return to -1 ;
}
int main()
{
int arr[]={
1,2,3,4,5,6,7,8,9,10,11}; // First of all, we need to set the array arr Sort
printf("%d \n",BinSearch(arr,(sizeof(arr)/sizeof(arr[0])),7));
return 0;
}
边栏推荐
- 北京大学|通过对比学习实现离线元强化学习的鲁棒任务表示
- 51万奖池邀你参战!第二届阿里云ECS CloudBuild开发者大赛来袭
- 0.0 - how can SolidWorks be uninstalled cleanly?
- Install some office tools
- 【深入理解TcaplusDB技术】TcaplusDB进程
- Georgia Institute of technology - coordinated coverage and tracking planning of multi UAV wildfire with service quality assurance
- AB打包有的Shader没有触发IPreprocessShaders的回调
- 元宇宙怎么就这么火,市场喊起来的10万亿是吹嘘还是真相?
- [deeply understand tcapulusdb technology] how to initialize and launch tcapulusdb machine
- 散列表(哈希表)
猜你喜欢

Nrf51822 peripheral learning

Nlp-d57-nlp competition D26 & skimming questions D13 & reading papers & finding bugs for more than an hour

华为云招募工业智能领域合作伙伴,强力扶持+商业变现

图的存储结构(邻接矩阵)
![[nfs failed to mount problem] mount nfs: access denied by server while mounting localhost:/data/dev/mysql](/img/15/cbb95ec823cdde5fb8f032dc45cfc7.png)
[nfs failed to mount problem] mount nfs: access denied by server while mounting localhost:/data/dev/mysql

记可视化项目代码设计的心路历程以及理解

树、森林及二叉树的相互转换

About Random Forest

51万奖池邀你参战!第二届阿里云ECS CloudBuild开发者大赛来袭

小甲鱼老师《带你学C带你飞》的后续课程补充
随机推荐
带超时的recv函数
漫话Redis源码之一百一二十
佐治亚理工学院|具有服务质量保证的多无人机野火协同覆盖和跟踪规划
Concordia University | volume product cycle network for reward generation in reinforcement learning
漫话Redis源码之一百一十九
小甲鱼老师《带你学C带你飞》的后续课程补充
mysql filesort要小心
Mysql database knowledge points (III)
【深入理解TcaplusDB技术】TcaplusDB事务管理——错误排查
Compilation error: /usr/bin/ld: /usr/local/lib/libgflags a(gflags.cc.o): relocation R_ X86_ 64_ 32S against `. rodata‘
MySQL约束
Altium Designer中off grid pin解决方法
0.0 - how can SolidWorks be uninstalled cleanly?
二叉排序树的查找、插入和删除
vs2008 水晶报表升级到 vs2013对应版本
【深入理解TcaplusDB技术】TcaplusDB新增机型
Huffman tree (C language)
优化了一半的SQL
Definitions and terms of drawings
iVX无代码挑战五秒游戏制作