0

所以我实现了一个使用二进制搜索方法搜索文件的工作程序:

public int BSearch(int x1, int x2) throws IOException {
    int current_key;

    middle=(x1+x2)/2;
    if(x1>x2) {
        middle=-1;  //middle==-1 is condition of 'key not found'
        return middle;
    }
    MyFile.seek(middle*4);
    current_key=MyFile.readInt();
    da++;
    if(current_key==key) {
        return middle;
    }
    else if(key<current_key) {
        x2=middle-1;
        return BSearch(x1,x2);
    }
    else {
        x1=middle+1;
        return BSearch(x1,x2);
    }
}

现在我想对其进行转换,以便将文件逐段(每次 1KB)读取到缓冲区中,然后对该缓冲区进行二进制搜索。如果在该缓冲区中找不到密钥,我会进一步读取文件,依此类推。我想澄清一下,缓冲区是这样的手工缓冲区(纠正我):

 byte[] buf = new byte[1024];
 MyFile.read(buf);
 ByteArrayInputStream bis= new ByteArrayInputStream(buf1);
 DataInputStream ois= new DataInputStream(bis);
 current_key=ois.readInt();

一个大问题(除其他外)是我不知道如何从缓冲区的某个位置读取

4

1 回答 1

0

好的,我想我设法通过将缓冲区逐个元素复制到一个新的 int[] 数组来做到这一点。我想相信它仍然比每次我想加载缓冲区时访问磁盘要快。

于 2013-03-18T08:14:40.173 回答