Previous Lecture Lecture 7 Next Lecture

Lecture 7, Tue 01/30

Algorithm Analysis, Recursion

Slides folder

Recorded video

Plan for today

Course-related announcements

Algorithm Analysis

The recording shows the details of what we discussed.

Additionally, these notes from Prof. Wang might be helpful as well:

Algorithm Analysis

import time

def f1(n):
	l = []
	for i in range(n):
		l.insert(0,i)
	return

def f2(n):
	l = []
	for i in range(n):
		l.append(i)
	return

print("starting f1")
start = time.time()
f1(200000)
end = time.time()
print("time elapsed: ", end - start, "seconds")

print("starting f2")
start = time.time()
f2(200000)
end = time.time()
print("time elapsed: ", end - start, "seconds")

Asymptotic Behavior

for i in range(10):
	print(i)
def f(n):
	for i in range(n):
		print(i)

Order of magnitude function (Big-O)

def f(n)
	x = 0
	for i in range(n):
		for j in range(n):
			x = i + j
def f(n):
    for i in range(n):
        return i

Recursion

The three laws of recursion

  1. A recursive algorithm must have a base case
  2. A recursive algorithm must change its state and move toward the base case
  3. A recursive algorithm must call itself, recursively

Code editor: Thonny