Lecture 08ppt
Lecture 08ppt
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
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
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
Algorithms – p. 141
Solving the Recurrence
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