Documentos de Académico
Documentos de Profesional
Documentos de Cultura
TA: Xiafong Mi
Lecture 1
Course will use the computer for communication and instruction as much as possible
Lecture 1
http://remus.rutgers.edu/cs112
Requirements
CS 111 or equivalent
Comfortable writing and debugging programs hundreds of lines long Java Language Arrays, Linked Lists
Lecture 1
Requirements
Written exams
Midterm and Final
Lecture 1
Textbook
Data Structures: An Object-Oriented Approach with Java, 2nd Edition by Sesh Venugopal McGraw-Hill Primis Custom Publishing
Lecture 1
Lecture 1
Lecture 1
10
Solutions
Count operations, not time Measure size of input Op count = f(input size) Among inputs of the same size, use worst or average op count Abstract away details of f: O(f)
focus on large inputs
Lecture 1
11
Example
Input: double array A, int n Output: largest number in first n elements of A
double big for (int i if (big < big = = A(0); = 1; i<n; i++){ A(i)){ A(i)}}
Lecture 1
12
Lecture 1
13
Size of input
Lecture 1
14
Count = f(size)
n-1 comparisons all inputs of same size same cost summarize n-1 as O(n)
Lecture 1
15
Another Example
Input: double array A, int n, double target Output: index in first n elements of A s.t. A(i)==target for (int i = 0; i<n && A(i)!= target; i++;){} Note average vs worst cases
Lecture 1
16
More Examples
for (int i = 0; i < m; i++){ for (int j = 0; j < n; j++){ sum += A(i, j)}} for (int i = 0; i < m; i++){ for (int j = 0; j < i; j++){ sum += A(i, j)}} for (int i = 0; i < m; i++){ for (int j = i-5; j < i; j++){ sum += A(i, j)}}
CS112: Slides for Prof. Steinbergs lecture Lecture 1
17
1+2++n
= n * (n + 1) / 2
CS112: Slides for Prof. Steinbergs lecture Lecture 1
18
Recursive example
int foo(int i){ if (i == 1){ system.out.println(foo); } else { foo(i-1); foo(i-1); }}
Lecture 1
19
Big O
80
CS112: Slides for Prof. Steinbergs lecture
300
Lecture 1
20
Asymptotically faster
Function g(n) grows asymptotically faster than f(n) if there is an n0 such that for all n>n0, g(n) > f(n)
n0
Lecture 1
21
Asymptotically faster
n0 Yes
n No
Lecture 1
22
Big O
What if one algorithm is run on a machine that is twice as fast as the other?
Lecture 1
23
Big O
f(n) is O(g(n)) if there is some constant c such that c*g(n) grows asymptotically faster than f(n) 3 * n2 + 7 is O(n2) because 4 * n2 grows asymptotically faster than 3 * n2 + 7
Lecture 1
24
Big O
Informally when we say f(n) is O(g(n)) we mean g(n) is the simplest function for which this is true
technically, n+4 is O(3*n2 - 9*n + 5) but we prefer to say n+4 is O(n)
Lecture 1
25
k is O(1)
341 is O(1)
k * f = O(f)
O(4*n4) = O(n4)
Lecture 1
26
Lecture 1
27