Algorithm 在1 GB内存中对10GB数据进行排序。我该怎么做?

Algorithm 在1 GB内存中对10GB数据进行排序。我该怎么做?,algorithm,Algorithm,问题是: 我的电脑只有1GB内存。我有一个10 GB数据的文本文件。这个文件包含数字。我将如何分类 添加更多细节 -They are all integers like 10000, 16723998 etc. -same integer values can be repeatedly appearing in the file. 将文件拆分为可以就地排序的部分(缓冲区) 然后,在对所有缓冲区进行排序时,同时取2个(或更多)并合并它们(如),直到只剩下1个缓冲区,即已排序的文件,请

问题是: 我的电脑只有1GB内存。我有一个10 GB数据的文本文件。这个文件包含数字。我将如何分类

添加更多细节

 -They are all integers like 10000, 16723998 etc.   
 -same integer values can be repeatedly appearing in the file.
将文件拆分为可以就地排序的部分(缓冲区)

然后,在对所有缓冲区进行排序时,同时取2个(或更多)并合并它们(如),直到只剩下1个缓冲区,即已排序的文件,请参见此。这家伙解释得很好

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.

对于仅使用1 GB RAM对10 GB数据进行排序:

  • 读取主内存中的1GB数据,并使用快速排序进行排序
  • 将排序后的数据写入磁盘
  • 重复步骤1和2,直到所有数据都在已排序的1GB块中(有10GB/1GB=10个块),现在需要将这些块合并到一个输出文件中
  • 将每个已排序块(1GB)的前90MB读入主内存中的输入缓冲区,并将剩余的100MB分配给输出缓冲区。 (为了获得更好的性能,我们可以将输出缓冲区调大,将输入缓冲区调小。)
  • 执行10路合并并将结果存储在输出缓冲区中
  • 每当输出缓冲区填满时,将其写入最终排序的文件并清空。 每当90 MB输入缓冲区中的任何一个为空时,用与其关联的1 GB排序块中的下一个90 MB填充该缓冲区,直到该块中没有更多可用数据为止
    这是一种在外部工作的外部合并排序方法。

    我们使用合并排序,首先对数据进行分割,然后进行合并

  • 将数据分成10组,每组大小为1gb
  • 对每个组进行排序并将其写入磁盘
  • 将每组中的10项加载到主内存中
  • 将最小的项目从主内存输出到磁盘。从所选项目的组中加载下一个项目
  • 循环步骤4,直到没有输出所有项目

  • 数字有多大?它们是整数还是任意精度?它们是什么格式的?这是一个有趣的谜题,但它缺少一些细节。仍然不清楚文件格式是什么。整数是否以32位签名并以10为基数写出,以空(\u0000)分隔?无符号64位,每个压缩8字节?另外,磁盘上有多少可用的暂存空间?这似乎不是一个任何类型的编程难题,因为这是一个标准问题(尽管我认为在这个内存丰富的时代,它得到的关注要少得多)。迁移到堆栈溢出。一个词:依赖虚拟内存。是的,mergesort是一种方法。(在起始块级别,您可以使用任何算法。)我最初也想过合并排序,但是合并n/2和n/2不会导致大小为n的排序数组吗?内存大小是这里的限制。因此,如果对2块1 GB缓冲区进行单独排序,则合并将由2 GB组成,这在内存中无法容纳。@saurabh这些缓冲区是您流式处理的文件,因此不需要加载完整的缓冲区memory@ratchetfreak,你是说,首先将文件的一部分读入缓冲区(1GB)并对缓冲区进行排序,然后将排序后的缓冲区写回文件?我必须使用外部合并排序,也就是说,将部分排序后的数据作为小块文件移动,然后进行合并排序。这个算法的顺序是什么?类似于合并排序?由于文件读写是特定于平台的,我们在计算顺序时可以忽略它们吗?忽略因子10,这个答案会添加到超链接的信息中还是被接受的信息中?令人遗憾的是,忽略一些事情已经变得司空见惯,比如生成尽可能少的运行次数,或者为每次合并留下一点反转来进行清理(如果内存可用,Knuth会同时处理这两个问题)。