Uploaded by 黃柏凱

Algorithms: Insertion Sort & Merge Sort Analysis

advertisement
----------------------------- 1 & 2 -----------------------------
1&2-2
2.2 Analyzing Algorithms
Introduction & Getting Started
Model
RAM: Random-access machine, in which each
memory access takes unit time and instructions
are executed one by one.
1.1 Algorithms
電腦可以執行
Algorithm: A sequence of computational steps
that transform the input of a computational
problem into the output.
Running time: number of steps, which is a
function of the input size.
Primitive operations
* best for n  30
2.1 Insertion Sort: An efficient algorithm for
sorting a small number of elements.
(memory access, +, -, *, /, ...)
a := b + c;
qsort(...)
Example: Insertion Sort
(Sort a hand of cards)
n
4 steps
1 step???
1&2-2a
Example : j=2
ok!
[b1]
j=3
j=4
j=5
j=6
1&2-1a
1
4
1
4
X
constants
i
j=6
A[6]
8
9
11
7
7
8
9
11
t6 = 4
1  tj  j
1&2-3
1&2-4
n
2.3 Designing Algorithms
j 2
Divide-and-Conquer :
T(n) = c1n+(c2+c4+c8)(n-1)+c5  t j +
(c6+c7)  t j  1
n
(into the same problems of
smaller size)
Conquer : recursively solve each subproblems
[bf5]
[b6]
Best-case:
Each tj=1. (The input A is sorted.)
=
=
Combine:
(c1+c2+c4+c5+c8)n-(c2+c4+c5+c8)
(n )
(rate of growth, order of growth)
*A simple example: Finding maximum
[b2][b3]
最大項
p
[b7]
k1n2 + k2n + k3
(n2)
=
=
r
p  r (n  1)
“termination condition”
(do nothing)
O(r-p+1)
Average-case: (Expected running time)
Each tj=j/2.
T(n)
q
Merge Sort
n2
=
=
1&2-4b
1&2-4c
Example:
Worst-case: (upper bound)
Each tj=j.
T(n)
*not similar
Divide:
j 2
T(n)
1&2-4a
[b4]
else
t1n2 + t2n + t3
(n2)
1&2-3a
1&2-5
1&2-5a
7
6
3
1
5
4
2
Figure 2.4 The operation of merge sort on the array A = 5, 2, 4, 7, 1, 3, 2, 6. The
lengths of the sorted sequences being merged increase as the algorithm progresses
from bottom to top[b8].
Analysis: (recurrence)
constant time
T(n)
=
=
(1)


2T (n / 2)  (n )
(n log n)
if n  1
if n  1
How to merge in Θ(n) time?
Homework: Pro. 2-1 and 2-4 .
[b9]
Download