如何执行二进制搜索的文本文件的 [英] How to perform a binary search of a text file

查看:156
本文介绍了如何执行二进制搜索的文本文件的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大的文本文件(5MB的),我在我的Andr​​oid应用程序中使用。我创建文件pre排序的字符串列表,并且一旦创建的文件不会改变。我怎么能执行这个文件的内容二进制搜索,不读书行由行查找匹配的字符串?

I have a big text file (5Mb) that I use in my Android application. I create the file as a list of pre-sorted Strings, and the file doesn't change once it is created. How can I perform a binary search on the contents of this file, without reading line-by-line to find the matching String?

推荐答案

由于文件的内容不会改变,可以打破文件分成多个部分。说A-G,H-N,0-T和U-Z。这可以让你检查的第一个字符,马上就能到组可能削减到四分之一的原始大小。现在,一个线性搜索不会采取或长或读取整个文件可能是一种选择。如果n / 4仍然过大,这个过程可能会延长,但想法是一样的。构建,而不是试图做这一切在内存中查找故障到文件结构。

Since the content of the file does not change, you can break the file into multiple pieces. Say A-G, H-N, 0-T and U-Z. This allows you to check the first character and immediately be able to cut the possible set to a fourth of the original size. Now a linear search will not take as long or reading the whole file could be an option. This process could be extended if n/4 is still too large, but the idea is the same. Build the search breakdowns into the file structure instead of trying to do it all in memory.

这篇关于如何执行二进制搜索的文本文件的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆