For example, let’s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste of time to copy the entire file into an array and then run binary search…I’ve effectively made it a linear time algorithm, because I’ll have to spend O(n) time copy the darn file before I can run my search.
Is there a faster way to do this? Is there maybe something like lseek which works with lines instead of bytes?
If there isn’t, am I better off just doing a linear search instead (assuming I’m only running the search once for the entire duration of my program) ?
You cannot seek by line. It’s pretty obvious once you think about it.
But you can do a sort-of binary search on a text file.
What you do is:
(This is best, I think, but you can use lseek and read if you must.)