sorting and reversing lines of a file

Doug Gwyn gwyn at smoke.BRL.MIL
Sat Jan 28 19:17:41 AEST 1989


In article <810031 at hpsemc.HP.COM> gph at hpsemc.HP.COM (Paul Houtz) writes:
>Umm, please explain why sorting isn't the most eficient way to do this.

Because the general sorting utility does more work than is
necessary to simply reverse the order of the records (lines).
It's of order N*logN whereas only 2*N is required for this task.

The best approach for a specialized implementation would be to
make a preliminary sequential scan to find the positions of the
start of each line (and probably the line length), then in the
second phase seek to the appropriate start places and copy out
the lines.  You should also implement a smart buffering scheme
to make this approach maximally efficient.



More information about the Comp.unix.questions mailing list