2
Time complexity of merge sort Krzysztof Bartoszek October 7, 2010 Algorithm 1 merge sort(list) if length(list)==1 then return list else A =merge sort(first half of list) B =merge sort(second half of list) C =merge(A,B) return C end if We will analyze the time complexity of the above algorithm. Define by a n as the time needed to sort a list of 2 n elements. The time complexity of the algorithm can be described by the following recursion, a n = 2a n-1 + c 1 2 n a 0 = c 0 . We need to solve this recursion to find an explicit dependence of the time on n and we will do this via its generating function A(x). A(x)= n=0 a n x n = c 0 + n=1 (2a n-1 + c 1 2 n )x n = c 0 + n=1 2a n-1 x n + n=1 c 1 (2x) n = = c 0 +2x n=1 a n-1 x n-1 + c 1 n=1 (2x) n = c 0 +2x n=0 a n x n + c 1 2x 1 1-2x = c 0 +2xA(x)+ 2c 1 x 1-2x , provided |x| < 0.5. This gives us that (1 - 2x)A(x) = c 0 + 2c 1 x 1-2x A(x) = c 0 1-2x + 2c 1 x (1-2x) 2 = = c 0 -c)1 1-2x + c 1 (1-2x) 2 . Using the formulas given in the lecture for the generating functions of different sequences, A(x)= c 0 -c)1 1-2x + c 1 (1-2x) 2 =(c 0 - c 1 ) n=0 (2x) n + c 1 n=0 ( n+1 1 ) (2x) n = =(c 0 - c 1 ) n=0 (2x) n + c 1 n=0 (n + 1)(2x) n = n=0 2 n (c 0 + c 1 n)x n . 1

Time complexity of merge sort - Matematiska vetenskaper ... · Time complexity of merge sort Krzysztof Bartoszek October 7, 2010 Algorithm 1 merge sort(list) if length(list)==1 then

  • Upload
    buitruc

  • View
    213

  • Download
    0

Embed Size (px)

Citation preview

Time complexity of merge sort

Krzysztof Bartoszek

October 7, 2010

Algorithm 1 merge sort(list)

if length(list)==1 thenreturn list

elseA =merge sort(first half of list)B =merge sort(second half of list)C =merge(A,B) return C

end if

We will analyze the time complexity of the above algorithm. Define by an asthe time needed to sort a list of 2n elements. The time complexity of the algorithmcan be described by the following recursion,

an = 2an−1 + c12n

a0 = c0.

We need to solve this recursion to find an explicit dependence of the time on nand we will do this via its generating function A(x).

A(x) =∑∞

n=0 anxn = c0 +

∑∞n=1(2an−1 + c12

n)xn = c0 +∑∞

n=1 2an−1xn +

∑n=1 c1(2x)n =

= c0 + 2x∑∞

n=1 an−1xn−1 + c1

∑∞n=1(2x)n = c0 + 2x

∑∞n=0 anx

n + c12x1

1−2x= c0 + 2xA(x) + 2c1x

1−2x,

provided |x| < 0.5. This gives us that

(1− 2x)A(x) = c0 + 2c1x1−2x

A(x) = c01−2x

+ 2c1x(1−2x)2

=

= c0−c)11−2x

+ c1(1−2x)2

.

Using the formulas given in the lecture for the generating functions of differentsequences,

A(x) = c0−c)11−2x

+ c1(1−2x)2

= (c0 − c1)∑∞

n=0(2x)n + c1

∑∞n=0

(n+1

1

)(2x)n =

= (c0 − c1)∑∞

n=0(2x)n + c1

∑∞n=0(n + 1)(2x)n =

∑∞n=0 2n(c0 + c1n)xn.

1

We therefore have that the formula for the sequence is,

an = (c0 + c1n)2n ≈ c1n2n = O(n2n).

Now let tk be the time needed to sort k = 2n elements,

tk = an = alog2 k = c1k log k = O(k log k).

Now for a general k > 8 (we don’t want to worry about small ks which would causeproblems in the argumentation below), let nk := min{n > 3 : 2n−1 ≤ k ≤ 2n}, i.e.2nk−1 ≤ k ≤ 2nk . We can bound the time complexity to sort a list of k elementsby the time needed to sort 2nk elements which is O(2nk log 2nk). Now we boundthe time for k from the bottom and above,

2nk−1 log 2nk−1 < k log k < 2nk log 2nk

2nk−1 log 2nk−1 < k log k < 2nk log 2nk < 2 · 2nk−1 log 2nk−12

2nk−1 log 2nk−1 < k log k < 2 · 2nk−12 · log 2nk−1

2nk−1 log 2nk−1 < k log k < 4 · 2nk−1 · log 2nk−1 < 4k log k ∈ O(k log k),

and as we are interested in getting complexity in terms of O(·) we get that thecomplexity of the merge sort algorithm is O(k log k) (we assumed k > 8 but wedon’t worry about small k).

2