{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CMPSC 20 F08 lecture notes: Algorithm Analysis

# CMPSC 20 F08 lecture notes: Algorithm Analysis - 2 Time...

This preview shows pages 1–2. Sign up to view the full content.

CS 20 WEEK 2: THURSDAY Algorithm analysis Useful identities. n i =0 2 i = 2 n +1 - 1 n i =0 i 2 i = 2 n i =0 C f ( i ) = C n i =0 f ( i ) x a x b = x ( a + b ) ( x a ) b = x a b 2 a + 2 a = 2 ( a +1) = 2 · 2 a log A ( B ) = log C ( B ) log C ( A ) “Counting iterations in a snippet of code”. Note. For “big O” notation, always discard all but the most significant growth factor sum=0; for(i = 0; i < n; i++) for(j = 0; j < n; j += 2) sum++; Time complexity: O ( n 2 ) sum=0; for(i = 0; i < n; i++) for(j = 0; j < n; j *= 2) sum++; Time complexity: O ( n log n ) sum=0; for(i = 0; i < n; i++) for(j = 0; j < i; j++) sum++; 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
englishCS 20 WEEK 2: THURSDAY
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 2 Time complexity: O ( n 2 ) sum=0; for(i = 0; i < n^2; i++) for(j = 0; j < i; j++) sum++; Time complexity: O ( n 4 ) sum=0; for(i = 0; i < n; i += 2) for(j = 0; j < i; j++) sum++; Time complexity: O ( n 2 ) Implementation notes Implementing a linked list with nested classes (Java). Last time: use a linked list to implement an array • void addElement(Object o, int index) • Object getElement(int index) • Constructor ListArray(int size)...
View Full Document

{[ snackBarMessage ]}

### Page1 / 2

CMPSC 20 F08 lecture notes: Algorithm Analysis - 2 Time...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online