所以我实现了一个使用二进制搜索方法搜索文件的工作程序:
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();
一个大问题(除其他外)是我不知道我将如何从缓冲区的某个位置读取
答案 0 :(得分:0)
好吧我想我设法通过将缓冲区复制到一个新的int []数组元素来实现。我想相信它每次我想加载缓冲区时仍然比访问光盘更快。