合并排序算法排序过程
外部分类 (External sorting)
External sorting is a technique in which the data is stored on the secondary memory, in which part by part data is loaded into the main memory and then sorting can be done over there. Then this sorted data will be stored in the intermediate files. Finally, these files will be merged to get a sorted data. Thus by using the external sorting technique, a huge amount of data can be sorted easily. In case of external sorting, all the data cannot be accommodated on the single memory, in this case, some amount of memory needs to be kept on a memory such as hard disk, compact disk and so on.
外部排序是一种将数据存储在辅助存储器中的技术,其中,将部分数据加载到主存储器中,然后可以在那里进行排序。 然后,将这些排序后的数据存储在中间文件中 。 最后,这些文件将合并以获得排序的数据。 因此,通过使用外部分类技术,可以轻松地分类大量数据。 在进行外部排序的情况下,所有数据都无法容纳在单个内存中,在这种情况下,需要在硬盘,光盘等内存中保留一定数量的内存。
The requirement of external sorting is there, where the data we have to store in the main memory does not fit into it. Basically, it consists of two phases that are:
存在外部排序的要求,我们必须存储在主存储器中的数据不适合其中。 基本上,它包括两个阶段:
Sorting phase: This is a phase in which a large amount of data is sorted in an intermediate file.
排序阶段:这是在中间文件中对大量数据进行排序的阶段。
Merge phase: In this phase, the sorted files are combined into a single larger file.
合并阶段:在此阶段,已排序的文件被合并为一个较大的文件。
One of the best examples of external sorting is external merge sort.
外部排序的最佳示例之一是外部合并排序。
外部合并排序 (External merge sort)
The external merge sort is a technique in which the data is stored in intermediate files and then each intermediate files are sorted independently and then combined or merged to get a sorted data.
外部合并排序是一种技术,其中数据存储在中间文件中,然后将每个中间文件独立排序,然后合并或合并以获得排序后的数据。
For example: Let us consider there are 10,000 records which have to be sorted. For this, we need to apply the external merge sort method. Suppose the main memory has a capacity to store 500 records in a block, with having each block size of 100 records.
例如:让我们考虑必须对10,000条记录进行排序。 为此,我们需要应用外部合并排序方法。 假设主存储器具有在一个块中存储500条记录的容量,每个块的大小为100条记录。
In this example, we can see 5 blocks will be sorted in intermediate files. This process will be repeated 20 times to get all the records. Then by this, we start merging a pair of intermediate files in the main memory to get a sorted output.
在此示例中,我们可以看到5个块将在中间文件中排序。 此过程将重复20次以获取所有记录。 然后,我们开始在主存储器中合并一对中间文件,以获得排序后的输出。
两路合并排序 (Two-Way Merge Sort)
Two-way merge sort is a technique which works in two stages which are as follows here:
双向合并排序是一项分两个阶段工作的技术,如下所示:
Stage 1: Firstly break the records into the blocks and then sort the individual record with the help of two input tapes.
阶段1 :首先将记录分成多个块,然后借助两个输入磁带对单个记录进行排序。
Stage 2: In this merge the sorted blocks and then create a single sorted file with the help of two output tapes.
第2阶段 :在此合并排序的块,然后借助两个输出磁带创建单个排序的文件。
By this, it can be said that two-way merge sort uses the two input tapes and two output tapes for sorting the data.
这样,可以说双向合并排序使用两个输入磁带和两个输出磁带对数据进行排序。
双向合并排序算法: (Algorithm for Two-Way Merge Sort:)
Step 1) Divide the elements into the blocks of size M. Sort each block and then write on disk.
步骤1)将元素划分为大小为M的块。对每个块进行排序,然后写入磁盘。
Step 2) Merge two runs
步骤2)合并两次跑步
Read first value on every two runs.
每两次运行读取第一个值。
Then compare it and sort it.
然后对其进行比较和排序。
Write the sorted record on the output tape.
将排序的记录写在输出磁带上。
Step 3) Repeat the step 2 and get longer and longer runs on alternates tapes. Finally, at last, we will get a single sorted list.
步骤3)重复步骤2,并在备用磁带上运行的时间越来越长。 最后,最后,我们将获得一个排序列表。
Analysis
分析
This algorithm requires log(N/M) passes with initial run pass. Therefore, at each pass the N records are processed and at last we will get a time complexity as O(N log(N/M).
该算法需要log(N / M)遍及初始运行遍。 因此,每遍处理N条记录,最后我们得到的时间复杂度为O(N log(N / M) 。
翻译自: https://www.includehelp.com/algorithms/external-merge-sorting.aspx
合并排序算法排序过程