Merge sort (mergesort)

First, the algorithm ideas:

  Divide and Conquer: vector LIST  

  Sequence into two   // O (1)

  Recursive sequence sorting   // 2 x T (n / 2 )

  The combined ordered sequence   // O (n)

 


 

Second, for example as follows:

  T (n) = 2 * T (n / 2) + O (n) T (n) = O (nlog (n)) can be solved using the replacement method; where O (n) are sorted merge two sub-sequences time.

  

 

  Merging merge principle as follows:

  

     Merging merge to achieve the following:

   Open space to store the element B _elem [lo, mi) in, and _elem [mi, hi) elements do not need to open up the space for a new cache, only need to define a pointer pointing to this memory C on it.

  

  Merging again improved:

  Because C is a half of the A and B when fully depleted, need not perform A [i ++] = C [k ++]; C is no need to wait depleted, once depleted B is terminated. Therefore, changing the order of the two loop body, remove redundant logic.

  

  Merging the complexity: the j + k as a whole, in the worst case, only O (n) is linear time.

  

 

Guess you like

Origin www.cnblogs.com/ccpang/p/11416256.html