Lec7InClass

Lec7InClass - 1) Find pieces of code whose time depends on...

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

View Full Document Right Arrow Icon
1) Find pieces of code whose time depends on problem size 2) Develop equation that expresses the # times loop executes in terms of problem size 3) Remove all but the fastest-growing term of each variable 4) Remove any constants in the term 5) express as O(term) for(i=0;i<n;i+=2) do something; 0 + 2 + 2 + …. + 2 >= n 0 + 2*k = n 2k = n k = n/2 n/2 -> O(n) for(i=1;i<n;i*=2) do something; 1 * 2 * 2 * … * 2 >= n 2^k = n k = log2(n) In CS, in general, logn means log2n O(logn) for(i=1;i<n;i*=3) do something log3(n) logn/log3 log(n) for(i=0;i<n;i++) for(j=0;j<m;j++) do something; n*m -> O(n*m) for(i=0;i<n;i++) for(j=0;j<n-i;j++) do something; n + (n-1) + (n-2) + …. . + 1 n*(n+1)/2 n^2/2 + n/2 n^2/2 n^2 O(n^2) class OrderedList{ Friend **friendarray; // an array of Friend * int count; int allocated; private: void expand(); public: OrderedList(); // constructor ~OrderedList(); // destructor void addFriend(Friend *f); void removeFriend(Friend *f); bool contains(Friend *f);
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/27/2011 for the course CMPSC 24 taught by Professor Agrawal during the Fall '09 term at UCSB.

Page1 / 3

Lec7InClass - 1) Find pieces of code whose time depends on...

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