0% found this document useful (0 votes)
6 views3 pages

02 Class

The document provides an introduction to algorithms, focusing on their definition, analysis, and design. It discusses specific algorithms like Insertion Sort and Merge Sort, detailing their running times and efficiency in sorting elements. Additionally, it covers concepts such as best-case, worst-case, and average-case scenarios in algorithm analysis.

Uploaded by

Kevin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

02 Class

The document provides an introduction to algorithms, focusing on their definition, analysis, and design. It discusses specific algorithms like Insertion Sort and Merge Sort, detailing their running times and efficiency in sorting elements. Additionally, it covers concepts such as best-case, worst-case, and average-case scenarios in algorithm analysis.

Uploaded by

Kevin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

----------------------------- 1 & 2 ----------------------------- 1&2-2

Introduction & Getting Started 2.2 Analyzing Algorithms


Model
1.1 Algorithms RAM: Random-access machine, in which each
memory access takes unit time and instructions
電腦可以執行
Algorithm: A sequence of computational steps are executed one by one.
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
(memory access, +, -, *, /, ...)
* best for n  30
2.1 Insertion Sort: An efficient algorithm for Example: Insertion Sort a := b + c; 4 steps
sorting a small number of elements. qsort(...) 1 step???
(Sort a hand of cards) n
1&2-2a
Example : j=2[b1]

ok!

j=3

j=4

constants
j=5
i j=6
A[6]
j=6
1 4 8 9 11 7
t6 = 4
X
1  tj  j
1 4 7 8 9 11
1&2-1a
1&2-3 1&2-4
n 2.3 Designing Algorithms
T(n) = c1n+(c2+c4+c8)(n-1)+c5 t j +
j 2 Divide-and-Conquer : [b4]
1&2-4a

 t j  1
n
(c6+c7) *not similar
j 2
Divide: (into the same problems of [bf5]

smaller size)
Best-case: Conquer : recursively solve each subproblems
[b6]

Each tj=1. (The input A is sorted.)


Combine:
T(n) = (c1+c2+c4+c5+c8)n-(c2+c4+c5+c8) *A simple example: Finding maximum 1&2-4b

= (n ) [b2][b3]
q
(rate of growth, order of growth) 1&2-4c

最大項 Example: Merge Sort


Worst-case: (upper bound) [b7] p r
Each tj=j. p  r (n  1)
n2 “termination condition”
T(n) = k1n2 + k2n + k3 (do nothing)
= (n2)
O(r-p+1)
Average-case: (Expected running time)
Each tj=j/2. else

T(n) = t1n2 + t2n + t3


= (n2)
1&2-3a
1&2-5

1&2-5a

3 6

1 2 4 5

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) if n  1

2T (n / 2)  (n ) if n  1
How to merge in Θ(n) time?
= (n log n)

Homework: Pro. 2-1 and 2-4 . [b9]

You might also like