3 Compare The Algorithms:, A,, A, Find The Maximum Value of
3 Compare The Algorithms:, A,, A, Find The Maximum Value of
1/8
§3 Compare the Algorithms
Algorithm 2
T( N ) = O( N2 )
2/8
§3 Compare the Algorithms
4 3 5 2 1 2 6 2
4 5 2 6
6 8
11
T T
O( N )
( N/2 ) ( N/2 )
4/8
§3 Compare the Algorithms
Algorithm 1 2 3 4
Time O( N3 ) O( N2 ) O(N log N) O( N )
N =10 0.00103 0.00045 0.00066 0.00034
N =100 0.47015 0.01112 0.00486 0.00063
Input
N =1,000 448.77 1.1233 0.05843 0.00333
Size N =10,000 NA 111.13 0.68631 0.03042
N =100,000 NA NA 8.0113 0.29832
Note:
Note:The
Thetime
timerequired
requiredto
toread
readthe
theinput
inputisisnot
notincluded.
included.
5/8
§4 Logarithms in the Running Time
〖 Example 〗 Binary Search:
Given: A [0] A [1] …… A [N 1] ; X
Task: Find X
Output: i if X = = A [ i ]
1 if X is not found
X ~ A [mid]
< == >
low high = mid 1 low= mid + 1 high
mid
6/8
§4 Logarithms in the Running Time
7/8
§5 Checking Your Analysis
When T(N) = O(N), check if T(2N)/T(N) 2
Method 1
When T(N) = O(N2), check if T(2N)/T(N) 4
When T(N) = O(N3), check if T(2N)/T(N) 8
……
8/8
Laboratory Project 1
Performance Measurement
Real Programmers
don't comment their code.
If it was hard to write,
I will not read and grade
it should be hard any
to understand
program which has
and harder to modify.
less than 30% lines
commented.