Dwell time complexity of merge sort process analysis ---- soft test

Merge sort method is to set a sequence number of n, is divided into two binary sequences, then the two sequences subdivided been points continues until a sequence of length n into 1. Then twenty-two by size merge. And so forth, until the last array is formed comprising a number of n.

Merge sort total time = time + exploded sequences sorted merge time time +

No matter how few are the trade-offs of each sequence decomposition, so the decomposition time is a constant and can be ignored.

Then: merge sort sequence Total time = time + sorted combined time


Assuming a number n of sequences ordered time T (n), T (n) is a function of the n and n varies with changes.

We will then sequence number of n, is divided into two (n / 2) sequences.

Then T (n) = 2 * T (n / 2) + combined time

Due to the merger, the two sub-groups within the sequence has been sorted, then we will be sorted two sequences are combined into one large ordered sequence, only one cycle if you can. if the number n of the cycle there is need to compare, so the time complexity is n.

Then T (n) = 2 * T (n / 2) + n

 

We then two n / 2 sequence is subdivided into four (n / 4) sequences.

A (n / 2) = time ordered sequence of two (n / 4) + merge sort time sequence of two (n / 4) to sequence a (n / 2) of the time sequence

T (n / 2) = 2 * T (n / 4) + n / 2

The T (n / 2) brought into T (n), T (n) = 2 * (2 * T (n / 4) + n / 2) + n,

By simplifying T (n) = 4 * T (n / 4) + 2n

 

We then four n / 4 sequence is subdivided into eight (n / 8) sequences.

T (n / 4) = 2 * T (n / 8) + n / 4

The T (n / 4) into a yellow formula, T (n) = 4 * (2 * T (n / 8) + n / 4) + 2n

By simplifying T (n) = 8 * T (n / 8) + 3n

······


Such points down, as we have said earlier, is divided into n sequences, each sequence there is only one number so far.

Earlier we assume there is a sequence of n number of ordered time T (n), and now only one number for each sequence, there is no need for the group sorting, the time complexity is 0. T (1) = 0

We have not found law, the right front of the formulas n coefficient increases with increasing the number of layers, the first layer is no formula n, the coefficient is 0, the coefficient of the second layer is n 1, the third layer is 2 the fourth layer is 3. Law then came out, in front of the index n is 1 minus the number of layers.

That this figure has not very familiar with, like, like a binary tree, binary tree through knowledge we know, a number of layers of a binary tree of n nodes (log2n) +1.

Then the index n minus 1 in front of the number of layers.

(log2n)+1-1=log2n

That log2n coefficient is the lowest level of n.

 

Then we last layer is not represented as such

T(n)=n*T(1)+(log2n)*n

T (1) = 0, then T (n) = (log2n) * n

So the time complexity of merge sort is nlog2n

Published 62 original articles · won praise 39 · views 120 000 +

Guess you like

Origin blog.csdn.net/liangjiabao5555/article/details/89670082