Lecture E Introduction To Algorithms
Lecture E Introduction To Algorithms
Introduction to Algorithms
Unit E1 – Basic Algorithms
Demonstrate the
notion of an
algorithm using two
classic ones
For j = 2 .. N-1
If j|N
Output “N is composite” and halt
Output “N is prime”
temp x y
After 0 rounds -- 72 120
After 1 round 72 120 72
After 2 rounds 48 72 48
After 3 rounds 24 48 24
After 4 rounds 0 24 0
Output: 24
Output: 1.40…
Analysis of Running
Times of Algorithms
r x/r
After 0 rounds 1 2
After 1 round 1.5 1.33..
After 2 rounds 1.41.. 1.41..
Output: 1.41…
Using and
understanding
Recursion
5 * factorial(4)
4 * factorial(3)
3 * factorial(2)
2 * factorial(1)
1 * factorial(0)
return 1
return 1
return 2
return 6
return 24
return 120