Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 454693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:17:47+00:00 2026-05-12T22:17:47+00:00

For example, let’s say I want to find a particular word or number in

  • 0

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) ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T22:17:47+00:00Added an answer on May 12, 2026 at 10:17 pm

    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:

    • Stat the file to get the length or seek to the end and get the position.
    • Memory map the file.
      (This is best, I think, but you can use lseek and read if you must.)
    • Seek to the middle of the file, minus your average line length. Just guess.
    • Scan forward for a newline, unless you are at position 0.
    • Read your line and compare.
    • Repeat for 1/4th or 3/4ths, 1/8th, 1/16th, etc.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is an example (let us say in a file named mybatch.bat ): @echo
For example let us say that we want to save a list of URLs
I want to search a word inside a string : For example let the
For Example: Let's say that I want to return a view that displays a
A simple example: Let's say I have one alias being sourced somewhere as: alias
Is there any way to get PDF document text language? Example: Let's say I
Let's take an example. A file1 -file has a content file1, its own name.
Let's take for example a single file committed in CVS with the following history.
Let's say for example I wanted to echo You are using Windows! or You
Please indulge my simplistic example: Let's say I have 1 million pages on my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.