0% found this document useful (0 votes)
2 views

Lecture 08ppt

The document explains the merge sort algorithm, detailing its divide and conquer phases, where the list is split into smaller pieces and then merged back together in sorted order. It provides the pseudocode for the merge sort and merge functions, along with an analysis of the algorithm's time complexity, concluding that the worst-case running time is Θ(n log n). The document also discusses the recurrence relation for the algorithm's running time and illustrates the pattern of its growth through examples.

Uploaded by

Taq Weem Tariq
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture 08ppt

The document explains the merge sort algorithm, detailing its divide and conquer phases, where the list is split into smaller pieces and then merged back together in sorted order. It provides the pseudocode for the merge sort and merge functions, along with an analysis of the algorithm's time complexity, concluding that the worst-case running time is Θ(n log n). The document also discusses the recurrence relation for the algorithm's running time and illustrates the pattern of its growth through examples.

Uploaded by

Taq Weem Tariq
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Merge sort

The divide phase: split the list top-down into


smaller pieces

7 5 2 4 1 6 3 0

7 5 2 4 1 6 3 0

7 5 2 4 1 6 3 0

7 5 2 4 1 6 3 0

split
Algorithms – p. 134
Merge sort

The conquer phase: merge the smaller lists


bottom up into larger pieces

0 1 2 3 4 5 6 7

2 4 5 7 0 1 3 6

5 7 2 4 1 6 0 3

7 5 2 4 1 6 3 0

merge
Algorithms – p. 135
Merge Sort Algorithm

MERGE - SORT( array A, int p, int r)


1 if (p < r)
2 then
3 q ← (p + r)/2
4 MERGE - SORT(A, p, q) // sort A[p..q]
5 MERGE - SORT(A, q + 1, r) // sort A[q + 1..r]
6 MERGE (A, p, q, r) // merge the two pieces

Algorithms – p. 136
Merge Sort Algorithm
MERGE (
array A, int p, int q int r)
1 int B[p..r]; int i ← k ← p; int j ← q + 1
2 while (i ≤ q) and (j ≤ r)
3 do if (A[i] ≤ A[j])
4 then B[k ++ ] ← A[i ++ ]
5 else B[k ++ ] ← A[j ++ ]
6 while (i ≤ q)
7 do B[k ++ ] ← A[i ++ ]
8 while (j ≤ r)
9 do B[k ++ ] ← A[j ++ ]
10 for i ← p to r
11 do A[i] ← B[i]
Algorithms – p. 137
Analysis of Merge Sort
• First, consider the running time of the
procedure merge(A, p, q, r).
• Let n = r − p + 1 denote the total length of
both left and right sub-arrays, i.e., sorted
pieces.
• The merge procedure contains four loops,
none nested in the other.
• Each loop can be executed at most n times.
• Thus running time of merge is Θ(n)

Algorithms – p. 138
Analysis of Merge Sort
• Let T (n) denote the worst case running time
of MergeSort on an array of length n.
• If we call MergeSort with an array containing
a single item (n = 1) then the running time is
constant.
• We can just write T (n) = 1, ignoring all
constants.

Algorithms – p. 139
Analysis of Merge Sort
• For n > 1, MergeSort splits into two halves,
sorts the two and then merges them together.
• The left half is of size dn/2e and the right half
is bn/2c.
• How long does it take to sort elements in sub
array of size dn/2e?
• We do not know this but because dn/2e < n
for n > 1, we can express this as T (dn/2e).

Algorithms – p. 140
Analysis of Merge Sort
• Similarly the time taken to sort right sub array
is expressed as T (bn/2c).
• In conclusion we have

1 if n = 1,
T (n) =
T (dn/2e) + T (bn/2c) + n otherwise

• This is called recurrence relation, i.e., a


recursively defined function.

Algorithms – p. 141
Solving the Recurrence

Let’s expand the terms.


T (1) = 1
T (2) = T (1) + T (1) + 2 = 1 + 1 + 2 = 4
T (3) = T (2) + T (1) + 3 = 4 + 1 + 3 = 8
T (4) = T (2) + T (2) + 4 = 8 + 8 + 4 = 12
T (5) = T (3) + T (2) + 5 = 8 + 4 + 5 = 17
...

Algorithms – p. 142
Solving the Recurrence

...
T (8) = T (4) + T (4) + 8 = 12 + 12 + 8 = 32
...
T (16) = T (8) + T (8) + 16 = 32 + 32 + 16 = 80
...
T (32) = T (16) + T (16) + 32 = 80 + 80 + 32 = 192

Algorithms – p. 143
Solving the Recurrence
What is the pattern here?
• Let’s consider the ratios T (n)/n for powers of
2:
T (1)/1 = 1 T (8)/8 = 4
T (2)/2 = 2 T (16)/16 = 5
T (4)/4 = 3 T (32)/32 = 6
• This suggests T (n)/n = log n + 1
• Or, T (n) = n log n + n which is Θ(n log n) (use
the limit rule).
Algorithms – p. 144

You might also like