140 likes | 244 Views
CS151 Lab 2. 9/15/2005. How to Turn In Assignments. Do not use the submission instructions associated with the test environment. These call for you to store your .java files in an encrypted .jar file and leave them in a public directory in your account. Too much trouble.
E N D
CS151 Lab 2 9/15/2005
How to Turn In Assignments • Do not use the submission instructions associated with the test environment. • These call for you to store your .java files in an encrypted .jar file and leave them in a public directory in your account. • Too much trouble. • Just email them to me. • Source code (.java files) • Probably best to .zip or .tgz them with other soft-copy files you send. • Both hard and soft copies.
Asymptotic Growth Rate(Order Notation) Definitions • f(n) = O(g(n)): • There exist positive constants c and N: • If n > N, f(n) < cg(n) • ie: for large n, f grows no faster than g.
Asymptotic Growth Rate(Order Notation) Definitions • f(n) = W(g(n)): • There exist positive constants c and N: • If n > N, f(n) > cg(n) • ie: for large n, f grows at least as fast (no slower than) than g.
Asymptotic Growth Rate(Order Notation) Definitions • f(n) = Q(g(n)): • f(n) = O(g(n)) and f(n) = W(g(n)) • There exist positive constants c1 and N1: • If n > N, f(n) < cg(n) • ie: for large n, f grows no faster than g. • There exist positive constants c2 and N2: • If n > N, f(n) > cg(n) • ie: for large n, f grows at least as fast than g. • f and g differ by a constant factor.
Asymptotic Growth Rate • In this class, we will generally not differentiate between f and g if f(n) = Q(g(n)). • Does this makes sense? • n2 + n + 10000 = n2 – 3? • en + n1000 = en+ n2?
Asymptotic Growth Rate • We rank or rate algorithms by their complexity. • Time complexity • Space complexity • Computers are fast... • Even really difficult (time-consuming) operations don’t take a lot of time on a modern computer on small input sets. • Suppose it takes 100ns to do it the right way and 1s to do it a wrong way. • You’ll still never notice. • But for a much larger-size input... • The same 2 algorithms would take 100s and 1 billion s! • You would definitely notice this. • At this scale, the large term of the function dominates.
Asymptotic Growth Rate • At these scales, one term of the function tends to dominate.
Asymptotic Growth Rate • Lets look at the functions in in-class exercise 1.7. • f(x) = x • f(x) = x2 • f(x) = log2(x) • f(x) = xlog2(x) • f(x) = 2x • f(x) = ex
In-class Exercise 1.6 • Go ahead and implement the “SelectionSort” algorithm • Use the files in the class notes • This does not use the test environment. • But you should be able to use the same function you write today (renamed) for the homework.
SelectionSort data[0] min Unsorted Data min data[0] Find Minimum Swap Now all but the top is unsorted
SelectionSort • So: • Let x = 0, y = (size of data) • 1: Find minimum in data[x..y] • 2: Swap the minimum with data[x] • 3: x += 1 • 4: Go to step 1 unless x = y
In-class Exercise 1.8 • Let’s try and prove: • loga(xy) = loga(x) + loga(y). • Don’t cheat and look at the solution.