# Class 29: Data Structures

Back to Project Discussion. On to The Design of Lists.

Held Thursday, October 14, 1999

Overview

Today we will move from the consideration of algorithms to the design of data structures: mechanisms for organizing information.

Notes

• Reminder: the Wednesday after break, each group needs to send me a set of interfaces and stub implementations for their part of the project. Each method in each interface must be accompanied by a ``6P'' (purpose, parameters, precondition, postconditions, problems/exceptions, and produces/returns).
• Are there any final questions on the exam?
• A few of you had convincing reasons to turn it in late, so please don't discuss it until you return.
• Yes, your `partition` method must be O(n).
• Yes, the end comment for partition is incorrect.
• In problem B, you only have to do the greatest difference in O(n). You can do the least difference in O(nlog2n) or O(n) or O(1) for extra credit.
• A number of you have noted that no matter what you do, Quicksort seem to sort 50, 1, 400, 1111, 2 ``correctly'', and instead produces 1, 1111, 2, 400, 50. However, Quicksort is using a `StringComparator` which does not look at the values as numbers. It looks at them as strings. Since the character ``4'' precedes the character ``5'', 400 precedes 500. (Try testing with strings instead of numbers or write a new comparator.)

Contents

Summary

• Four key issues: functionality, implementation, efficiency, applications
• Two basic data structures
• Arrays
• Collections

## Data Structures

• As we develop more interesting algorithms, we start to find it necessary to develop ways to store and organize data.
• In fact, the way in which we organize data can affect the running time of an algorithm.
• As we've already seen, O(log2n) binary search requires that we have a structure that we can split in half (or simulate splitting in half) in constajnt time.
• In assignment 3, we considered an algorithm in which we had to deal with lengths of sidewalks between pairs of buildings. How might we store such information?
• The structures used to organize the data in the program are called, not so surprisingly, data structures.
• For the second half of the semester, we will consider a number of data structures and some related algorithms.
• Note that there are a number of issues we will consider in describing data structures:
• Their functionality: what ``methods'' the structures provide, as it were.
• Their implementation: how they do what they do.
• Their efficiency: how fast they are (or with how much memory they consume)
• Their applciations: how we might use them.
• To emphasize the separation between functionality and implementation, we will (almost) always begin by describing structures as interfaces.

### Arrays

• We'll begin by reconsidering arrays.
• What are the key operations that you might use to describe arrays?
• Don't read the next few lines until we've had a chance to discuss them.
• We want to create new arrays
• Input: size of the array
• Input: type of the elements in the array
• We want to set elements of arrays
• Running time: O(1)
• We want to get elements of arrays
• Running time: O(1)
• We want to get the size of arrays
• Running time: O(1)
• What else?

### Collections

• Most data structures are used to collect a number of objects (just like arrays).
• Hence, most data structures we create will be variations (subinterfaces) of a general `Collection` interface.
• What methods might such an interface include? We'll need to consider purpose and detailed meaning (perhaps even the legendary ``6 P's''.
• Here are some basics (without detailed definitions):
• We might create collections.
• We might add elements to the collection.
• We might delete elements from the collection.
• We might check if there is room for more elements.
• We might clear all the elements in the collection.
• Are there others you can think of?

## History

Tuesday, 10 August 1999

• Created as a blank outline.

Wednesday, 13 August 1999

• Filled in the details. Many were copied from outline 24 of CS152 99S.
• Added introductory section on data structures.
• Reformatted and reorganized.

Friday, 15 August 1999

• Removed uncovered section on lists.

Back to Project Discussion. On to The Design of Lists.

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.