in reply to read and sort multiple files

Pre-sort each individual files (so you never have more than one in memory at a time), then just merge the files together. If a file is too big to be sorted, split it into smaller files first.

This is merge sort.

Replies are listed 'Best First'.
Re^2: read and sort multiple files
by matrixmadhan (Beadle) on Dec 01, 2008 at 06:46 UTC
    Instead of using merge-sort alone ( as a plain approach ) a hybrid way of using in-memory sorting and merge-sort can be combined and used together.

    For ex:

    Out of the total 'n' number of files, sort only 'm' files in memory

    ( m could be approximately be m ~ n/2, this is just approximation and can increase/decrease based on the in-memory available and threshold value of memory permitted to be used by the process in consideration )



    With the approach of combining merge-sort and in-memory sort

    1) Both merge-sort and the quickness of in-memory sort are being used

    2) No problem of too many files taking too much of memory, as sorting the number of files in memory is now controlled

      Instead of using merge-sort alone ( as a plain approach ) a hybrid way of using in-memory sorting and merge-sort can be combined and used together.

      That's what the post to which you replied already suggested.

      Out of the total 'n' number of files, sort only 'm' files in memory

      A 100MB file takes up pretty major chunk of memory already. Remember, if the array isn't preallocated to hold enough lines, twice the size of the data is needed.

      If I were to re-implement the work in Perl, I'd probably do something equivalent to

      1. cat * | (cd tmp; split --lines=XXX -bytes=YYY - chunk)
        This maximizes memory usage while limiting memory usage.
      2. for f in tmp/chunk* ; do sort $f >$f.sorted ; done
        The sorting would actually be done before writing out the chunk.
      3. Merge file pairs until only one file remains.

      Update: I struck out a statement that's probably wrong. There is overhead, but it should be proportional to the number of lines, not number of bytes.

        This maximizes memory usage while limiting memory usage.

        Sorry, I don't understand the above comment at all. Would you mind explaining that?

        2. for f in tmp/chunk* ; do sort $f >$f.sorted ; done The sorting would actually be done before writing out the chunk.

        This cannot be guaranteed, what if the file is too big