用于查找书籍的智能蛮力算法?

a smart brute force algorithm for finding a book?

我想编写一个蛮力算法来尝试在图书馆中找到一本书。如果你要找的书不在,你要花多长时间才能算出来? 任何智能暴力方法?

我想在图书馆书名所在的列表中搜索书名,并从列表的中心开始?

您应该在 why GNU grep is fast 上阅读这篇文章并尝试他的方法。简而言之,他说:

  • Use Boyer-Moore (and unroll its inner loop a few times).

  • Roll your own unbuffered input using raw system calls. Avoid copying the input bytes before searching them. (Do, however, use buffered output. The normal grep scenario is that the amount of output is small compared to the amount of input, so the overhead of output buffer copying is small, while savings due to avoiding many small unbuffered writes can be large.)

  • Don't look for newlines in the input until after you've found a match.

  • Try to set things up (page-aligned buffers, page-sized read chunks, optionally use mmap) so the kernel can ALSO avoid copying the bytes.

The key to making programs fast is to make them do practically nothing. ;-)

Grep 完全可以满足您的要求:暴力搜索无序数据——他的算法很聪明,尽管它是暴力搜索。