external sorting

前端 未结 2 1658
南笙
南笙 2020-12-25 09:12

in this web page:

http://web.eecs.utk.edu/~huangj/CS302S04/notes/external-sorting2.html

Merge the resulting runs together into successively

2条回答
  •  盖世英雄少女心
    2020-12-25 09:37

    Imagine you have the numbers 1 - 9

    9  7  2  6  3  4  8  5  1
    

    And let's suppose that only 3 fit in memory at a time.

    So you'd break them into chunks of 3 and sort each, storing each result in a separate file:

    279
    346
    158
    

    Now you'd open each of the three files as streams and read the first value from each:

    2 3 1
    

    Output the lowest value 1, and get the next value from that stream, now you have:

    2 3 5
    

    Output the next lowest value 2, and continue onwards until you've outputted the entire sorted list.

提交回复
热议问题