当前位置:网站首页>顺序表的折半查找法
顺序表的折半查找法
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;
}

边栏推荐
- PHP replaces the key of a two-dimensional array with a specified element value
- The whole page turns gray
- Recyclerview scrolls to the specified location
- When MySQL queries fields in JSON format, it takes a property value of JSON data
- Figure explanation of fiborache sequence
- sudo: ulimit: command not found
- (4) Pyqt5 tutorial -- > Custom signal and slot (super winding...)
- Mysql database logs binlog save aging (expire\u logs\u days)
- The first techo day Tencent technology open day in 2022 will be held online on June 28
- Thinkphp3 count ` *'problem
猜你喜欢

Upgrade opsenssh to 8.8p1

(4) Pyqt5 tutorial -- > Custom signal and slot (super winding...)

Embedded software development written examination and interview notes (latest update: February 17, 2022)

SDN system method | 9 Access network

Penetration tool environment - installing sqli labs in centos7 environment

Happy shopkeeper source code -- Introduction to happy shopkeeper system development mode

Shell learning notes (latest update: 2022-02-18)

Go novice exploration road 1

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

ECSHOP video list_ ECSHOP uploading video, video classification, video list playing video function
随机推荐
Repair the error that ECSHOP background orders prompt insufficient inventory when adding goods. Please reselect
Explain AHP in human language (very detailed principle + simple tool implementation)
Is it safe to open an account and buy stocks on the Internet?
Zhangxiaobai's road of penetration (VI) -- the idea and process of SQL injection and the concat series functions and information of SQL_ Schema database explanation
Mind mapping video
Penetration tool environment - installing sqli labs in centos7 environment
JQ verifies whether the input color is legal
Laravel excel export
Flutter common commands and problems
ThinkPHP upload image compression size
The R language cartools package divides data, the scale function scales data, and the NaiveBayes function of e1071 package constructs a naive Bayesian model
Micro engine generates QR code
ECSHOP whole site custom URL supports directory type
Huile optimization system -- sharing of secondary development source code of huile optimization app system
R language uses ordinal or. The display function obtains the summary statistical information of the ordered logistic regression model (the odds ratio and its confidence interval corresponding to the v
Polling and long polling
Laravel multi project mutual access
Shell learning notes (latest update: 2022-02-18)
Node child processes and threads
揭秘GaussDB(for Redis):全面對比Codis