折半查找 binarySearch

上传:liujian41830 浏览: 35 推荐: 0 文件:CPP 大小:442B 上传时间:2019-04-09 11:27:24 版权申诉
A binary search algorithm (or binary chop) is a technique for finding a particular value in a sorted list. It makes progressively better guesses, and closes in on the sought value, by comparing an element halfway with what has been determined to be an element too low in the list and one too high in the list. A binary search finds th e median element in a list, compares its value to the one you are searching for, and determines if it’s greater than, less than, or equal to the one you want. A guess that turns out to be too high becomes the new top of the list, and one too low the new bottom of the list. The binary search's next guess is halfway between the new list's top and bottom. Pursuing this strategy iteratively, it narrows the search by a factor 2 each time, and finds your value. A binary search is an example of a divide and conquer algorithm (more specifically a decrease and conquer algorithm) and a dichotomic search (more at Search algorithm). The most common application of binary search is to find a specific value in a sorted list. To cast this in the frame of the guessing game (see Example below), realize that we are now guessing the index, or numbered place, of the value in the list. This is useful because, given the index, other data structures will contain associated information. Suppose a data structure containing the classic collection of name, address, telephone number and so forth has been accumulated, and an array is prepared containing the names, numbered from one to N. A query might be: what is the telephone number for a given name X. To answer this the array would be searched and the index (if any) corresponding to that name determined, whereupon it would be used to report the associated telephone number and so forth. Appropriate provision must be made for the name not being in the list (typically by returning an index value of zero), indeed the question of interest might be only whether X is in the list or not. If the list of names is in sorted order, a binary search will find a given name with far fewer probes than the simple procedure of probing each name in the list, one after the other in a linear search, and the procedure is much simpler than organising a hash table though that would be faster still, typically averaging just over one probe. This applies for a uniform distribution of search items but if it is known that some few items are much more likely to be sought for than the majority then a linear search with the list ordered so that the most popular items are first may do better. The binary search begins by comparing the sought value X to the value in the middle of the list; because the values are sorted, it is clear whether the sought value would belong before or after that middle value, and the search then continues through the correct half in the same way. Only the sign of the difference is inspected: there is no attempt at an interpolation search based on the size of the differences. Your task is to write a program that, given a set numbers of ascending and a key, finding a particular postion in a sorted list. e median element in a list, compares its value to the one you are searching for, and determines if it’s greater than, less than, or equal to the one you want. A guess that turns out to be too high becomes the new top of the list, and one too low the new bottom of the list. The binary search's next guess is halfway between the new list's top and bottom. Pursuing this strategy iteratively, it narrows the search by a factor 2 each time, and finds your value. A binary search is an example of a divide and conquer algorithm (more specifically a decrease and conquer algorithm) and a dichotomic search (more at Search algorithm). The most common application of binary search is to find a specific value in a sorted list. To cast this in the frame of the guessing game (see Example below), realize that we are now guessing the index, or numbered place, of the value in the list. This is useful because, given the index, other data structures will contain associated information. Suppose a data structure containing the classic collection of name, address, telephone number and so forth has been accumulated, and an array is prepared containing the names, numbered from one to N. A query might be: what is the telephone number for a given name X. To answer this the array would be searched and the index (if any) corresponding to that name determined, whereupon it would be used to report the associated telephone number and so forth. Appropriate provision must be made for the name not being in the list (typically by returning an index value of zero), indeed the question of interest might be only whether X is in the list or not. If the list of names is in sorted order, a binary search will find a given name with far fewer probes than the simple procedure of probing each name in the list, one after the other in a linear search, and the procedure is much simpler than organising a hash table though that would be faster still, typically averaging just over one probe. This applies for a uniform distribution of search items but if it is known that some few items are much more likely to be sought for than the majority then a linear search with the list ordered so that the most popular items are first may do better. The binary search begins by comparing the sought value X to the value in the middle of the list; because the values are sorted, it is clear whether the sought value would belong before or after that middle value, and the search then continues through the correct half in the same way. Only the sign of the difference is inspected: there is no attempt at an interpolation search based on the size of the differences. Your task is to write a program that, given a set numbers of ascending and a key, finding a particular postion in a sorted list.
上传资源
用户评论
相关推荐
折半查找验证
大学数据结构,折半查找的验证的实现代码,基础基础基础
CPP
0B
2019-02-20 18:07
C折半查找
折半查找,可以直接运行,欢迎大家下载
0B
2019-02-28 01:13
JS折半查找
对JS数组查询添加折半查询算法功能载入时有点慢是因为初始化创建一个100000的数组并填充数据使用方法:arrayName.select(value),程序自动判断数组是顺序或降序(仅支持顺
HTML
0B
2019-03-09 03:25
递归折半查找
二分查找又称折半查找,优点是比较次数少,查找速度快,平均性能好;其缺点是要求待查表为有序表,且插入删除困难。因此,折半查找方法适用于不经常变动而查找频繁的有序列表。首先,假设表中元素是按升序排列,将表
ZIP
0B
2019-03-09 03:25
折半查找实例
Find half of the instance
CPP
0B
2019-06-22 17:02
折半查找代码
折半查找是一种数据结构算法非常有用我们用C语言实现了查找简单有效
CPP
0B
2019-07-07 23:30
折半查找.txt
折半查找代码.txt
TXT
0B
2019-07-30 00:49
折半查找算法
前几天做题才想起来的折半查找算法,其实也不难,自己仔细想想也就会了,实在不行就上博客或者是论坛去查询资料就行了。学习编程语言也是这样的呀,遇到不会的就去图书馆或者是网上去查找自己所需要的东西。
TXT
0B
2019-09-18 22:04
datastructure折半查找
折半查找递归及非递归
SLN
0B
2019-07-23 23:09
折半查找.asm
汇编课上折半查找的代码,有助于计算机和网络专业的学生
ASM
0B
2019-09-27 20:32
顺序查找折半查找
本程序共包含2个查找程序,分别是顺序查找和折半查找。
ZIP
0B
2019-03-16 21:34
顺序查找折半查找
用顺序存储结构表示查找表,实现以下运算要求:(1)建立一个整数数据文件datafile;(2)从文件datafile读取数据,并导入一维数组中;(3)用顺序查找方法查找指定元素(由键盘输入),并显示查
CPP
0B
2019-06-05 13:19
顺序和折半查找
顺序和折半查找 已验证 好啊 !#includetypedef struct{ int elem[50] ; int length; }SSTable; int Search_Seq(SSTable
DOC
0B
2019-03-16 21:33
递归折半查找
递归折半查找法基本的程序思想,初学者可以参考一下。
C
0B
2019-01-16 00:26
折半查找案例使用
对折半查找的具体应用,以及冒泡排序!
RAR
0B
2019-05-31 11:26