# lecture18 - Last time Dynamic storage recap Expandable...

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

Last time: * Dynamic storage recap * Expandable arrays * Asymptotic analysis Today: * linked structures & Simple lists 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.

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

View Full Document
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.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern