[go: up one dir, main page]

0% found this document useful (0 votes)
13 views31 pages

1-Chapter One - Analysis of Algorithms

The document provides an analysis of the Merge Sort algorithm, detailing its recursive sorting method and the merging process of two sorted arrays. It includes a recurrence relation for the time complexity of Merge Sort, which is T(n) = 2T(n/2) + Θ(n), leading to an overall complexity of Θ(n log n). The conclusion emphasizes that Merge Sort outperforms Insertion Sort in terms of efficiency, especially for larger datasets.

Uploaded by

mersha abdisa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views31 pages

1-Chapter One - Analysis of Algorithms

The document provides an analysis of the Merge Sort algorithm, detailing its recursive sorting method and the merging process of two sorted arrays. It includes a recurrence relation for the time complexity of Merge Sort, which is T(n) = 2T(n/2) + Θ(n), leading to an overall complexity of Θ(n log n). The conclusion emphasizes that Merge Sort outperforms Insertion Sort in terms of efficiency, especially for larger datasets.

Uploaded by

mersha abdisa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 31

Chapter 1 – Analysis of

Algorithms
Merge Sort

 MERGE-SORT A[1 . . n]
 If n = 1, done.
 Recursively sort A[ 1 . . n/2 ] and A[ n/2+1 . . n ] .
 “Merge” the 2 sorted lists.

 Key subroutine: MERGE


Merging Two Sorted Arrays

20 12
13 11
7 9
2 1
Merging Two Sorted Arrays

20 12
13 11
7 9
2 1

1
Merging Two Sorted Arrays

20 12 20 12
13 11 13 11
7 9 7 9
2 1 2

1
Merging Two Sorted Arrays

20 12 20 12
13 11 13 11
7 9 7 9
2 1 2

1 2
Merging Two Sorted Arrays

20 12 20 12 20 12
13 11 13 11 13 11
7 9 7 9 7 9
2 1 2

1 2
Merging Two Sorted Arrays

20 12 20 12 20 12
13 11 13 11 13 11
7 9 7 9 7 9
2 1 2

1 2 7
Merging Two Sorted Arrays

20 12 20 12 20 12 20 12
13 11 13 11 13 11 13 11
7 9 7 9 7 9 9
2 1 2

1 2 7
Merging Two Sorted Arrays

20 12 20 12 20 12 20 12
13 11 13 11 13 11 13 11
7 9 7 9 7 9 9
2 1 2

1 2 7 9
Merging Two Sorted Arrays

20 12 20 12 20 12 20 12 20 12
13 11 13 11 13 11 13 11 13 11
7 9 7 9 7 9 9
2 1 2

1 2 7 9
Merging Two Sorted Arrays

20 12 20 12 20 12 20 12 20 12
13 11 13 11 13 11 13 11 13 11
7 9 7 9 7 9 9
2 1 2

1 2 7 9 11
Merging Two Sorted Arrays

20 12 20 12 20 12 20 12 20 12 20 12
13 11 13 11 13 11 13 11 13 11 13
7 9 7 9 7 9 9
2 1 2

1 2 7 9 11
Merging Two Sorted Arrays

20 12 20 12 20 12 20 12 20 12 20 12
13 11 13 11 13 11 13 11 13 11 13
7 9 7 9 7 9 9
2 1 2

1 2 7 9 11 12
Merging Two Sorted Arrays

20 12 20 12 20 12 20 12 20 12 20 12
13 11 13 11 13 11 13 11 13 11 13
7 9 7 9 7 9 9
2 1 2

1 2 7 9 12
11

Time = (n) to merge a total of n elements (linear time).


Analyzing Merge Sort

T(n) MERGE-SORT A[1 . . n]


(1) 1.If n = 1, done.
Abuse
2T(n/2) 2.Recursively sort A[ 1 . . n/2]
3. and A[ n/2+1 . . n ] .
(n) 4.“Merge” the 2 sorted lists

 Sloppiness: Should be T( n/2 ) + T( n/2) , but it turns out not


to matter asymptotically.
Recurrence for Merge Sort

(1) if n = 1;
T(n) =
2T(n/2) + (n) if n > 1.

 We shall usually omit stating the base case when T(n) = (1)
for sufficiently small n, but only when it has no effect on the
asymptotic solution to the recurrence.
Recursion Tree

 Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.


Recursion Tree

 Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.


T(n)
Recursion Tree

 Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

cn
T(n/2) T(n/2)
Recursion Tree

 Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

cn

cn/2 cn/2

T(n/4) T(n/4) T(n/4) T(n/4)


Recursion Tree

 Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

cn

cn/2 cn/2
cn/4 cn/4 cn/4 cn/4

(1)
Recursion Tree

 Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

cn

cn/2 cn/2
h = lg n cn/4 cn/4 cn/4 cn/4

(1)
Recursion Tree

 Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

cn
cn
cn/2 cn/2
h = lg n cn/4 cn/4 cn/4 cn/4

(1)
Recursion Tree

 Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

cn
cn
cn/2 cn/2 cn
h = lg n cn/4 cn/4 cn/4 cn/4

(1)
Recursion Tree

 Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

cn
cn
cn/2 cn/2 cn
h = lg n cn/4 cn/4 cn/4 cn/4 cn

(1)
Recursion Tree

 Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

cn
cn
cn/2 cn/2 cn
h = lg n cn/4 cn/4 cn/4 cn/4 cn

(1) #leaves = n (n)


Recursion Tree

 Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

cn
cn
cn/2 cn/2 cn
h = lg n cn/4 cn/4 cn/4 cn/4 cn

(1) #leaves = n (n)


Total  (n lg n)
Conclusions

 (n lg n) grows more slowly than (n2).


 Therefore, merge sort asymptotically beats insertion sort in
the worst case.
 In practice, merge sort beats insertion sort for n > 30 or so.
Question & Answer

08/26/25 30
Thank You !!!

08/26/25 31

You might also like