lecture18 - Last time: * Dynamic storage recap * Expandable...

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

View Full Document Right Arrow Icon
Last time: * Dynamic storage recap * Expandable arrays * Asymptotic analysis Today: Suppose a client creates an IntSet of capacity 1, and then inserts N elements into it, using the doubling version of grow. Give a tight upper bound on the number of integer values that will be copied over the lifetime of this function in the worst case? Well, in the worst case we copied N-1 elements to insert the Nth. However, the copy before that was only for the N/2'th element, which copied (N/2 - 1) elements. Likewise for the N/4th element, etc. So, the total number of copies is: T = (N-1) + (N/2 - 1) + (N/4 - 1) + (N/8 - 1) + . .. We can drop the "-1" terms in each step: T < N + N/2 + N/4 + N/8 + . .. So, the big question is how many entries are there in this series before we terminate. Note that we will eventually terminate, since we can only copy an integral number of integers. So, let's start collapsing terms to see where we get: T < N + (N/2 + N/4) + N/8 + . .. < N + 3N/4 + N/8 + . .. < N + (3N/4 + N/8) + . .. < N + 7N/8 + . .. See the pattern? In the worst case, this becomes: < N + (reallyBigNumber - 1)N/(reallyBigNumber) But (reallyBigNumber-1)/reallyBigNumber is almost 1, so T < N + N < 2N So, instead of copying almost (N^2-N)/2 elements, we copy fewer than 2N of them. Here's a little table showing what this means: # elements (N^2-N)/2 2N 1 0 2 8 28 16 64 2016 128 512 130816 1024 2048 2096128 4096 So you can see, the "double" implementation is *much* better than the "by-one" implementation.
Background image of page 1

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

View Full DocumentRight Arrow Icon
This notion of comparing two alternative implementations at a very coarse-grained level is the entire point of 281, 477, and several other classes. This is what making programs faster is all about. We won't see much more of it in 280. +++++++++++++++++++++++++++++++++++++++++++++++++++++ Expandable arrays are only one way to implement storage that can grow and shrink over time. Another way is to use a "linked structure". A linked structure is one with a series of zero or more data containers, connected by pointers from one to another, like so: +---+ +---+ +---+ +---+ list-----> | -----> | -----> | -----> | -----\ +---+ +---+ +---+ +---+ \ --- - A linked structure is sort of like a freight train. If you need to carry more freight, you get a new boxcar, connect it to the train, and fill it. When you don't need it any more, you can remove that boxcar from the train. Suppose we wanted to implement an abstract data type representing a mutable list of integers, represented as a linked structure. This ADT is similar to the list_t type from project one, except that those lists were immutable: once a list_t object was created, no operations on that list would ever change it. A valid list of integers is either: the empty list, or an integer
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/04/2008 for the course EECS 215 taught by Professor Phillips during the Winter '08 term at University of Michigan.

Page1 / 9

lecture18 - Last time: * Dynamic storage recap * Expandable...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online