Data Structures Algorithms 简明教程

Merge Sort Algorithm

归并排序是一种基于分而治之技术的排序技术。其最坏情况时间复杂度为 Ο(n log n),是使用和涉及到的算法中最多的一种。

Merge sort is a sorting technique based on divide and conquer technique. With worst-case time complexity being Ο(n log n), it is one of the most used and approached algorithms.

归并排序先将数组分成相等的两个部分,然后按顺序合并它们。

Merge sort first divides the array into equal halves and then combines them in a sorted manner.

How Merge Sort Works?

为了理解归并排序,我们以下面的形式获取一个未排序的数组:

To understand merge sort, we take an unsorted array as the following −

unsorted array

我们知道归并排序算法首先对整个数组进行迭代,将其平均分成两半,直到达到最小的单元。在示例中,一个包含 8 个元素的数组被平均分成包含 4 个元素的两个数组。

We know that merge sort first divides the whole array iteratively into equal halves unless the atomic values are achieved. We see here that an array of 8 items is divided into two arrays of size 4.

divides array

这并不改变原始数组中元素出现的顺序。现在,我们将这两个数组平均分成两半。

This does not change the sequence of appearance of items in the original. Now we divide these two arrays into halves.

two arrays into halves

我们进一步对这些数组进行平均分,而且进一步将其分成最小的单元,无法再进行进一步的划分。

We further divide these arrays and we achieve atomic value which can no more be divided.

atomic value

现在,我们按照打破元素时的顺序将它们合并在一起。请注意这些列表给出的色标。

Now, we combine them in exactly the same manner as they were broken down. Please note the color codes given to these lists.

我们首先比较每个列表中的元素,然后按照排序的方式将它们合并到另一个列表中。我们看到 14 和 33 处于排序后的位置。我们比较 27 和 10,在包含 2 个值的目标列表中,我们首先把 10 放入,然后是 27。我们更改 19 和 35 的顺序,而 42 和 44 则按顺序排列。

We first compare the element for each list and then combine them into another list in a sorted manner. We see that 14 and 33 are in sorted positions. We compare 27 and 10 and in the target list of 2 values we put 10 first, followed by 27. We change the order of 19 and 35 whereas 42 and 44 are placed sequentially.

compare element

在合并阶段的下一轮迭代中,我们比较包含两个数据值的列表,并将其合并到一个包含找到的数据值的列表中,按照排序的顺序进行排列。

In the next iteration of the combining phase, we compare lists of two data values, and merge them into a list of found data values placing all in a sorted order.

sorted order

经过最终合并之后,该列表变得有序,并被视为最终的解决方案。

After the final merging, the list becomes sorted and is considered the final solution.

merge sort

Merge Sort Algorithm

归并排序算法持续对列表进行平均分,直到无法再进行进一步的划分。根据定义,如果列表中仅包含一个元素,则认为它是排序的。然后,归并排序算法结合较小的排序列表,使新列表也保持排序。

Merge sort keeps on dividing the list into equal halves until it can no more be divided. By definition, if it is only one element in the list, it is considered sorted. Then, merge sort combines the smaller sorted lists keeping the new list sorted too.

Step 1: If it is only one element in the list, consider it already
sorted, so return.
Step 2: Divide the list recursively into two halves until it can no
more be divided.
Step 3: Merge the smaller lists into new list in sorted order.

Pseudocode

现在,我们来看归并排序函数的伪代码。由于算法指出了两个主要函数,即分而治之和合并。

We shall now see the pseudocodes for merge sort functions. As our algorithms point out two main functions − divide & merge.

归并排序算法采用递归,我们将按照相同的方式查看其实现。

Merge sort works with recursion and we shall see our implementation in the same way.

procedure mergesort( var a as array )
   if ( n == 1 ) return a
      var l1 as array = a[0] ... a[n/2]
      var l2 as array = a[n/2+1] ... a[n]
      l1 = mergesort( l1 )
      l2 = mergesort( l2 )
      return merge( l1, l2 )
end procedure
procedure merge( var a as array, var b as array )
   var c as array
   while ( a and b have elements )
      if ( a[0] > b[0] )
         add b[0] to the end of c
         remove b[0] from b
      else
         add a[0] to the end of c
         remove a[0] from a
      end if
   end while
   while ( a has elements )
      add a[0] to the end of c
      remove a[0] from a
   end while
   while ( b has elements )
      add b[0] to the end of c
      remove b[0] from b
   end while
   return c
end procedure

Example

在以下示例中,我们逐一演示了归并排序算法。首先,对每次迭代的数组进行平均分,分成两个子数组,直到子数组仅包含一个元素。当无法进一步对子数组进行平均分时,便执行合并操作。

In the following example, we have shown Merge-Sort algorithm step by step. First, every iteration array is divided into two sub-arrays, until the sub-array contains only one element. When these sub-arrays cannot be divided further, then merge operations are performed.

Merge Sort algorithm

Analysis

我们来考虑一下归并排序算法的运行时间,即 T(n) 。因此,

Let us consider, the running time of Merge-Sort as T(n). Hence,

\mathrm{T\left ( n \right )=\left\{\begin{matrix} c & if\, n\leq 1 \\ 2\, xT\left ( \frac{n}{2} \right )+dxn &otherwise \\ \end{matrix}\right.}\:where\: c\: and\: d\: are\: constants

因此,使用此递归关系,

Therefore, using this recurrence relation,

T\left ( n \right )=2^{i}\, T\left ( n/2^{i} \right )+i\cdot d\cdot n

如同 \:\: i=log\: n,\: T\left ( n \right )=2^{log\, n}T\left ( n/2^{log\, n} \right )+log\, n\cdot d\cdot n

As,\:\: i=log\: n,\: T\left ( n \right )=2^{log\, n}T\left ( n/2^{log\, n} \right )+log\, n\cdot d\cdot n

=c\cdot n+d\cdot n\cdot log\: n

因此,\: \: T\left ( n \right ) = O(n\: log\: n ).

Therefore,\: \: T\left ( n \right ) = O(n\: log\: n ).

Example

以下是不同编程语言中上述方法的实现 -

Following are the implementations of the above approach in various programming languages −