LectureNote_02 (1)
LectureNote_02 (1)
(Lecture Note 2)
Introduction to
Algorithm (2)
2025. 3. 7
• Definition
For all the N satisfying N ≥ N0, if T(N) ≤ C0f(N), then T(N) = O(f(N))
* T(N) : function for the processing time
For example,
T(N) = 3*N*N + 1, f(N) = N*N, C0 = 4 O(N2)
void main()
{
int sum = 0; 1 operation
int j; 1 operation
for( j=1; j<=30; j++) 30 operations 63 operations
{
sum += j; 30 operations O(63)
}
return sum;
}
1 1 1 1 1 1 1
log2N 3 6 9 13 16 19
③ GCD of A and 0 = A
Practice
④ GCD of A and B =
GCD of mod(A,B) and B Make C programs for GCD !
#include <Windows.h>
Algorithm
Measuring
time
Minus method :
Modulus method :
Recursion method :
return TRUE;
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
3. 1. If array[i] != 0, go to Step 3
3. 2. For( j=i+i; j<=N; j+=i)
3. 3. Assign 1 to array[ j]
4. For i=2 to N, print out if array[i] = 0