You are on page 1of 31

11/01/22 1

Tarun Jain
Analysis of Merge Sort

11/01/22 Tarun Jain 2


Divide-and-Conquer
 Technique (or paradigm) involves:
 “Divide” stage: Express problem in terms
of several smaller subproblems
 “Conquer” stage: Solve the smaller
subproblems by applying solution
recursively – smallest subproblems may
be solved directly
 “Combine” stage: Construct the solution
to original problem from solutions of
smaller subproblem
11/01/22 Tarun Jain 3
Merge Sort Strategy
n
(unsorted)
 Divide stage: Split the n-
element sequence into two
subsequences of n/2 n/2 n/2
elements each (unsorted) (unsorted)

 Conquer stage: MERGE SORT MERGE SORT


Recursively sort the two
subsequences n/2 n/2
(sorted) (sorted)

 Combine stage: Merge the


two sorted subsequences MERGE
into one sorted sequence
(the solution) n
(sorted)

11/01/22 Tarun Jain 4


Execution Example

• Partition
7 2 9 43 8 6
1

2 9 4 8 6 1

11/01/22 Tarun Jain 5


Execution Example (cont.)

• Recursive call, partition


7 2 9 43 8 6 1

7294

2 9 4 8 6 1

11/01/22 Tarun Jain 6


Execution Example (cont.)

• Recursive call, partition


7 2 9 43 8 6 1

7294

72

2 9 4 8 6 1

11/01/22 Tarun Jain 7


Execution Example (cont.)

• Recursive call, base case


7 2 9 43 8 6 1

7294

72

77 2 9 4 8 6 1

11/01/22 Tarun Jain 8


Execution Example (cont.)

• Recursive call, base case


7 2 9 43 8 6 1

7294

72

77 2 9 4 8 6 1
2
11/01/22 Tarun Jain 9
Execution Example (cont.)

• Merge
7 2 9 43 8 6 1

7294

722 7

77 2 9 4 8 6 1
2
11/01/22 Tarun Jain 10
Execution Example (cont.)

• Recursive call, …, base case, merge


7 2 9 43 8 6 1

7294

722 7 9 4  49

77 2 99 4 8 6 1
2 4
11/01/22 Tarun Jain 11
Execution Example (cont.)

• Merge
7 2 9 43 8 6 1

7 29 4 2 4 7 9

722 7 9 4  49

77 2 99 4 8 6 1
2 4
11/01/22 Tarun Jain 12
Execution Example (cont.)

• Recursive call, …, merge,


merge
7 2 9 43 8 6 1

7 29 4 2 4 7 9 3 8 6 1  1 3 6 8

722 7 9 4  49 3 8  38 6 1  16

77 2 99 4 33 8 66 1


2 4 8 1
11/01/22 Tarun Jain 13
CS1501, CSE DEPT, SCIT, MUJ
Execution Example (cont.)

• Merge
7 2 9 43 8 6 1  1 2 3 4 6 7 89

7 29 4 2 4 7 9 3 8 6 1  1 3 6 8

722 7 9 4  49 3 8  38 6 1  16

77 2 99 4 33 8 66 1


2 4 8 1
11/01/22 Tarun Jain 14
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Merging Sorted Sequences


• Combines the sorted
(1) subarrays A[p..q] and
A[q+1..r] into one sorted
array A[p..r]
(n)
• Makes use of two working
arrays L and R which
initially hold copies of the
(1) two subarrays
• Makes use of sentinel
value () as last element
to simplify logic
(n)

11/01/22 Tarun Jain 15


Merge-Sort: Merge
Sorted

A:

merge
Sorted Sorted
FirstPart SecondPart

A:

11/01/22 A[left] A[middle] A[right] 16


Merging
p q r
1 2 3 4 5 6 7 8

2 4 5 7 1 2 3 6

 Input: Array A and indices p, q, r such


that p ≤ q < r
 Subarrays A[p . . q] and A[q + 1 . . r] are sorted
 Output: One single sorted subarray
A[p . . r]

11/01/22 Tarun Jain


Merge-Sort: Merge Example

A: 2
5 3
5 7 28
15 8 30
1 4
6 5 14
10 6

L: R:
3 5 15 28 6 10 14 22

Temporary Arrays
11/01/22 Tarun Jain 18
Merge-Sort: Merge Example

A:
3
1 5 15 28 30 6 10 14

k=0

L: R:
3
2 15
3 28
7 30
8 6
1 10
4 14
5 22
6

i=0 j=0
11/01/22 Tarun Jain 19
Merge-Sort: Merge Example

A:
1 2
5 15 28 30 6 10 14

k=1

L: R:
3
2 5
3 15
7 28
8 6
1 10
4 14
5 22
6

i=0 j=1
11/01/22 Tarun Jain 20
Merge-Sort: Merge Example

A:
1 2 3 28 30
15 6 10 14

k=2

L: R:
2 3 7 8 6
1 10
4 14
5 22
6

i=1 j=1
11/01/22 Tarun Jain 21
Merge-Sort: Merge Example

A:
1 2 3 4 6 10 14

k=3

L: R:
2 3 7 8 6
1 10
4 14
5 22
6

i=2 j=1
11/01/22 Tarun Jain 22
Merge-Sort: Merge Example

A:
1 2 3 4 5 6 10 14

k=4

L: R:
2 3 7 8 6
1 10
4 14
5 22
6

i=2 j=2
11/01/22 Tarun Jain 23
Merge-Sort: Merge Example

A:
1 2 3 4 5 6 10 14

k=5

L: R:
2 3 7 8 6
1 10
4 14
5 22
6

i=2 j=3
11/01/22 Tarun Jain 24
Merge-Sort: Merge Example

A:
1 2 3 4 5 6 7 14

k=6

L: R:
2 3 7 8 6
1 10
4 14
5 22
6

i=2 j=4
11/01/22 Tarun Jain 25
Merge-Sort: Merge Example

A:
1 2 3 4 5 6 7 8
14

k=7

L: R:
3
2 5
3 15
7 28
8 6
1 10
4 14
5 22
6

i=3 j=4
11/01/22 Tarun Jain 26
Merge-Sort: Merge Example

A:
1 2 3 4 5 6 7 8

k=8

L: R:
3
2 5
3 15
7 28
8 6
1 10
4 14
5 22
6

i=4 j=4
11/01/22 Tarun Jain 27
Merge Sort Algorithm

(1)
T(n/2)
T(n/2)
(n)

So T(n) = (1) when n = 1, and


2T(n/2) + (n) when n > 1

11/01/22 Tarun Jain 28


Recurrences
• The expression:
 c n1

T (n)  
2T 
n
   cn n1
2
• 
is a recurrence.
 Recurrence: an equation that describes a function in
terms of its value on smaller functions
11/01/22 29

Tarun Jain
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Analysis of Merge Sort


Analysis of recursive calls …

11/01/22 Tarun Jain 30


Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Analysis of Merge Sort

T(n) = cn(lg n + 1)
= cnlg n + cn

11/01/22 Tarun Jain 31

You might also like