Documente Academic
Documente Profesional
Documente Cultură
6 18 56 62 1 9 15 43
Merging two sorted subsequences
Unsorted
6 18 56 62 1 9 15 43
Sorted Sorted
Merging two sorted subsequences
6 18 56 62 1 9 15 43
Merging
Merging two sorted subsequences
6 18 56 62 1 9 15 43
Merging
Left half
Right half
1
Left half
Right half
1
Left half
Right half
1 6
Left half
Right half
1 6
Left half
Right half
1 6 9
Left half
Right half
1 6 9
Left half
Right half
1 6 9 15
Left half
Right half
1 6 9 15
Left half
Right half
1 6 9 15 18
Left half
Right half
1 6 9 15 18
Left half
Right half
1 6 9 15 18 43
Left half
Right half
1 6 9 15 18 43
Left half
Right half
1 6 9 15 18 43 56 62
Left half
Right half
1 6 9 15 18 43 56 62
Left half
Right half
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
Merge Sort
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
Merge Sort
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
Merge Sort
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
Merge Sort
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
Merge Sort
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 6 32 15 43 1 9 22 26 19 55 37 43 2 99
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
Merge Sort
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 6 32 15 43 1 9 22 26 19 55 37 43 2 99
Merge Sort
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 6 32 15 43 1 9 22 26 19 55 37 43 2 99
Merge Sort
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
6 18 26 32 1 9 15 43 19 22 26 55 2 37 43 99
18 26 6 32 15 43 1 9 22 26 19 55 37 43 2 99
Merge Sort
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
6 18 26 32 1 9 15 43 19 22 26 55 2 37 43 99
Merge Sort
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
6 18 26 32 1 9 15 43 19 22 26 55 2 37 43 99
Merge Sort
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
1 6 9 15 18 26 32 43 2 19 22 26 37 43 55 99
6 18 26 32 1 9 15 43 19 22 26 55 2 37 43 99
Merge Sort
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
1 6 9 15 18 26 32 43 2 19 22 26 37 43 55 99
Merge Sort
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
18 26 32 6 43 15 9 1 22 26 19 55 37 43 99 2
1 6 9 15 18 26 32 43 2 19 22 26 37 43 55 99
Merge Sort
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1 2 6 9 15 18 19 22 26 26 32 37 43 43 55 99
1 6 9 15 18 26 32 43 2 19 22 26 37 43 55 99
Merge Sort
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1 2 6 9 15 18 19 22 26 26 32 37 43 43 55 99
Merge Sort
MergeSort (A, p, r) // sort A[p..r] by divide & conquer
1 if p < r
2 then q (p+r)/2 //divide
3 MergeSort (A, p, q) //conquer
4 MergeSort (A, q+1, r) //conquer
5 Merge (A, p, q, r) //combine: merge A[p..q] with A[q+1..r]
Merge Sort:
[Divide]
[Conquer]
[Combine]
Analysis of Merge Sort
Statement Cost
MergeSort (A, p, r) T(n)
1 if p < r (1)
2 then q (p+r)/2 (1)
3 MergeSort (A, p, q) T(n/2)
4 MergeSort (A, q+1, r) T(n/2)
5 Merge (A, p, q, r) (n)
Guess: T(n) = n lg n + n.
Induction:
• Basis: n = 1 n lgn + n = 1 = T(n).
• Hypothesis: T(k) = k lg k + k for all k < n.
• Inductive Step: T(n) = 2 T(n/2) + n
= 2 ((n/2)lg(n/2) + (n/2)) + n
= n (lg(n/2)) + 2n
= n lg n – n + 2n
= n lg n + n
Recursion-tree Method
•Making a good guess in substitution method is sometimes
difficult
•Use recursion trees to devise good guesses.
Recursion Trees
•Show successive expansions of recurrences using trees.
•Keep track of the time spent on the subproblems of a divide and
conquer algorithm.
•Help organize the algebraic bookkeeping necessary to solve a
recurrence.
Recursion Tree – Example
Running time of Merge Sort:
T(n) = (1) if n = 1
T(n) = 2T(n/2) + (n) if n > 1
Rewrite the recurrence as
T(n) = c if n = 1
T(n) = 2T(n/2) + cn if n > 1
c > 0: Running time for the base case and
time per array element for the divide and
combine steps.
Recursion Tree for Merge Sort
For the original problem, Each of the size n/2 problems has
we have a cost of cn, plus a cost of cn/2 plus two
two subproblems each of subproblems, each costing T(n/4).
size (n/2) and running time
T(n/2).
cn
cn
Cost of divide
and merge.
cn/2 cn/2
T(n/2) T(n/2)
T(n/4) T(n/4) T(n/4) T(n/4)
Cost of sorting
subproblems.
Recursion Tree for Merge Sort
Continue expanding until the problem size reduces to 1.
cn cn
cn/2 cn/2 cn
lg n
c c c c c c cn
Total : cnlgn+cn
Asymptotic notations
• O-notation
52
Big-O Visualization
53
Examples
• 2n2 = O(n3):
2n2 ≤ cn3 2 ≤ cn c = 1 and n0= 2
• n2 = O(n2):
n2 ≤ cn2 c ≥ 1 c = 1 and n0= 1
• 1000n2+1000n = O(n2):
• n = O(n2):
n ≤ cn2 cn ≥ 1 c = 1 and n0= 1
54
More Examples
• Show that 30n+8 is O(n).
• Show c,n0: 30n+8 cn, n>n0 .
• Let c=31, n0=8. Assume n>n0=8. Then
cn = 31n = 30n + n > 30n+8, so 30n+8 < cn.
55
Big-O example, graphically
• Note 30n+8 isn’t
less than n
anywhere (n>0).
• It isn’t even
less than 31n cn =
31n 30n+8
Value of function
everywhere.
• But it is less than
31n everywhere to
the right of n=8. 30n+8
n
O(n)
n>n0=8
Increasing n
56
No Uniqueness
• There is no unique set of values for n0 and c in proving the
asymptotic bounds
for all n ≥ 5
58
Examples
• 5n2 = (n)
59
Asymptotic notations (cont.)
• -notation
60
Master Theorem