问题在于: 我的计算机只有1GB内存。我有一个10 GB数据的文本文件。这个文件包含数字。我该如何对它们进行排序?
添加更多细节。
-They are all integers like 10000, 16723998 etc.
-same integer values can be repeatedly appearing in the file.
答案 0 :(得分:11)
将文件拆分为可以就地排序的部分(缓冲区)
然后当所有缓冲区被排序时,当时取2(或更多)并合并它们(如merge sort)直到只剩下1个缓冲区,这将是已排序的文件
答案 1 :(得分:6)
Knuth提出的外部排序怎么样? see 4.1,Wikipedia或TAOCP, Sorting and Searching。
答案 2 :(得分:2)
请参阅此link。这个家伙解释得很美。
An example of disk-based application: External mergesort algorithm (wikipedia)
A merge sort divides the unsorted list into n sublists, each containing 1 element, and then repeatedly merges sublists to produce new sorted sublists until there is only 1 sublist remaining.
The external mergesort algorithm sorts chunks that each fit in RAM, then merges the sorted chunks together.For example, for sorting 900 megabytes of data using only 100 megabytes of RAM:
1. Read 100 MB of the data in main memory and sort by some conventional sorting method, like quicksort.
2. Write the sorted data to disk.
3. Repeat steps 1 and 2 until all of the data is in sorted 100 MB chunks (there are 900MB / 100MB = 9 chunks), which now need to be merged into one single output file.
4. Read the first 10 MB of each sorted chunk (of 100 MB) into input buffers in main memory and allocate the remaining 10 MB for an output buffer. (In practice, it might provide better performance to make the output buffer larger and the input buffers slightly smaller.)
5. Perform a 9-way merge and store the result in the output buffer. Whenever the output buffer fills, write it to the final sorted file and empty it. Whenever any of the 9 input buffers empties, fill it with the next 10 MB of its associated 100 MB sorted chunk until no more data from the chunk is available. This is the key step that makes external merge sort work externally -- because the merge algorithm only makes one pass sequentially through each of the chunks, each chunk does not have to be loaded completely; rather, sequential parts of the chunk can be loaded as needed.
答案 3 :(得分:0)
将10GB缓冲区拆分为10 * 1GB缓冲区 以heap(最小或最大)处理所有10 GB数据一次,然后我们将在min_heap中留下1gb的已排序数据和9 gb的未排序数据... 然后对9GB数据进行相同操作 获得所有排序...
答案 4 :(得分:0)
仅使用1 GB RAM来排序10 GB数据:
这是在外部起作用的外部合并排序方法。
答案 5 :(得分:0)
我们使用归并排序先将数据划分然后合并。