CMPSC 20 F08 lecture notes: Algorithm Analysis

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

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

View Full Document Right Arrow Icon
CS 20 WEEK 2: THURSDAY Algorithm analysis Useful identities. n X i =0 2 i = 2 n +1 - 1 n X i =0 i 2 i = 2 n X i =0 C f ( i ) = C n X 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
Background image of page 1

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

View Full DocumentRight Arrow Icon
englishCS 20 WEEK 2: THURSDAY
Background image of page 2
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

This note was uploaded on 12/10/2008 for the course CS 20 taught by Professor Agrawal during the Fall '08 term at UCSB.

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 Right Arrow Icon
Ask a homework question - tutors are online