二分查找法,查找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字母比较上手吧!
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇:没有了
- 下一篇:没有了