当前位置:网站首页>顺序表的折半查找法
顺序表的折半查找法
2022-06-25 12:08:00 【yyy_zxc】
#include "seqlist.cpp" //包含顺序表基本算法
//折半查找算法
int BinSearch(RecType R[],int n,KeyType k){
int low=0,high=n-1,mid,count=0;
while(low<=high){ //当low>high时,表示查找失败
mid=(low+high)/2;
printf("第%d次比较:在【%d,%d】中比较元素R[%d]:%d\n",
++count,low,high,mid,R[mid].key);
if(R[mid].key==k) //查找成功返回
return mid+1;
if(R[mid].key > k) //继续在R[low...mid-1]中查找
high=mid-1;
else //继续在R[mid+1...high]中查找
low=mid+1;
}
return 0;
}
int main(){
RecType R[MAXL];
KeyType k = 9;
int a[]={1,2,3,4,5,6,7,8,9,10};
int i,n=10;
CreateList(R,a,n); //创建顺序表
printf("关键字序列:");
DispList(R,n);
printf("查找%d的比较过程如下:\n",k);
if((i=BinSearch(R,n,k))!=0)
printf("元素%d的位置是%d\n",k,i);
else
printf("元素%d不在表中\n",k);
return 1;
}

边栏推荐
- ECSHOP product attribute color specification size stock item No. automatic combination
- 揭秘GaussDB(for Redis):全面对比Codis
- PHP takes the difference set of two arrays
- Swagger document generated by node project API in vscode
- Shell learning notes (latest update: 2022-02-18)
- Zhangxiaobai's way of penetration (III) -- detailed explanation of SQL injection vulnerability principle (SQL Server)
- visual studio2019链接opencv
- Gradle knowledge points
- ECSHOP commodity wholesale multi attribute multi specification multi inventory batch purchase ECSHOP wholesale plug-in ECSHOP multi attribute order
- Concise H5 error page
猜你喜欢

Execution order of MySQL query statements join, on and where

(2) Pyqt5 tutorial -- > using qtdesigner to separate interface code

Installation and removal of MySQL under Windows

一篇文章讲清楚MySQL的聚簇/联合/覆盖索引、回表、索引下推

Windows下MySQL的安装和删除

冷启动的最优解决方案

Full nanny tutorial of Market Research Competition (experience sharing)

(3) Pyqt5 tutorial -- > signal and slot preliminary test

揭秘GaussDB(for Redis):全面對比Codis

Navicat premium view password scheme
随机推荐
Zhangxiaobai's way of penetration (V) -- detailed explanation of upload vulnerability and parsing vulnerability
Spicy food advertising e-commerce system development function and spicy food advertising e-commerce app system development source code sharing
Is it safe to open an account and buy stocks on the Internet?
PHP parsing QR code content
An article clearly explains MySQL's clustering / Federation / coverage index, back to table, and index push down
Service charge and time setting code sharing involved in crmeb withdrawal process
Explain AHP in human language (very detailed principle + simple tool implementation)
Concise H5 error page
Renrenyue -- renrenyue system development source code sharing
Kotlin Foundation
Mind mapping video
Laravel excel export
Arm V7 coprocessor
Mysql database logs binlog save aging (expire\u logs\u days)
New and old cluster migration of Minio data
The difference between this and super and their respective functions
Uncover gaussdb (for redis): comprehensive comparison of CODIS
Laravel multi project mutual access
Recyclerview scrolls to the specified location
2022 meisai e topic ideas sharing + translation