二分查找法,查找string

整理文件的时候发现的,以前的老代

list为word_cnt类型的数组,str为要查找的关键字,len,为数组中有效内容的个数

int	binSea(word_cnt* list, string& str, int len)
{
	int step = 0;	//d
	std::streamsize precision = cout.precision();	//d
	int low = 0;
	int high = len - 1;
	int mid = low + ((high - low) >> 1);
	while (low <= high)
	{
		cout << "step:" <<std::setw(3)<<step++ 
				<<" low:"<<std::setw(6)<<low
				<<"  mid:"<<std::setw(6)<<mid
				<<"  high:" <<std::setw(6)<< high << endl;	//d
		int result = str.compare(list[mid].word) ;
		if (result > 0)
		{
			low = mid + 1;
		}
		else if (result < 0)
		{
			high = mid - 1;
		}
		else
		{
			std::setw(precision);	//d

			return mid;
		}
		mid = low +( (high - low) >> 1);
	}
	std::setw(precision);	//d
	return -1;
}
运行结果:

voice_abc.txt opened !
we have read 132900 words into memory!
input key string:
help
key string=help
step:  0 low:     0  mid: 66449  high:132899
step:  1 low:     0  mid: 33224  high: 66448
step:  2 low: 33225  mid: 49836  high: 66448
step:  3 low: 49837  mid: 58142  high: 66448
step:  4 low: 49837  mid: 53989  high: 58141
step:  5 low: 53990  mid: 56065  high: 58141
step:  6 low: 56066  mid: 57103  high: 58141
step:  7 low: 57104  mid: 57622  high: 58141
step:  8 low: 57104  mid: 57362  high: 57621
step:  9 low: 57104  mid: 57232  high: 57361
step: 10 low: 57233  mid: 57297  high: 57361
step: 11 low: 57233  mid: 57264  high: 57296
step: 12 low: 57233  mid: 57248  high: 57263
help find in count_list[57248]
used time:57ms
请按任意键继续. . .
读入字典排序的 132,900个单词,用二分法查找来查找关键字。
顺便统计了一下占词比,结果如下:
voice_abc.txt opened !
we have read 132900 words into memory!
a*              7.7306%
b*              6.1257%
c*              9.6132%
d*              6.1648%
e*              4.4342%
f*              3.8736%
g*              3.7931%
h*              4.0241%
i*              3.4184%
j*              1.0399%
k*              1.8157%
l*              3.5162%
m*              6.0278%
n*              2.3649%
o*              2.2972%
p*              8.2039%
q*              0.51543%
r*              3.8315%
s*              9.1738%
t*              5.3416%
u*              1.6208%
v*              1.8119%
w*              2.0971%
x*              0.15425%
y*              0.48457%
z*              0.52596%
c*              max precent:9.6132%
请按任意键继续. . .
以c打头的单词,最多可能是c字母比较上手吧!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值