CS261 Data Structures Fall 2009 Professor Timothy Budd.

24
CS261 Data Structures Fall 2009 Professor Timothy Budd

Transcript of CS261 Data Structures Fall 2009 Professor Timothy Budd.

CS261

Data Structures

Fall 2009

Professor Timothy Budd

Why data structures

• By this point, you have learned the mechanics of coding - variables, loops, etc

• This course will help you take the next step in becoming a programming, learning higher level skills, such as abstraction, modularization, correctness, efficiency.

Administrivia

• Go over administrivia -

• Two midterms, one final

• Several programming assignments, several homeworks

• Recitations used for learning programming skills, course will deal more with concepts

More administrivia

• I’m found in KEC 3049

• OH: MWF 1:30 - 3:00 for now

• Two TA’s: they will each hold office hours as well in KEC lobby (see web page)

Textbook

• Textbook is on-line, also with links to many articles (wikipedia and so on) for further information. You should print it out

• Textbook begins with material you should already have seen - read chapters 1 to 5 immediately.

Active Learning

• This course uses a technique termed Active Learning

• We do worksheets most every day.

• Worksheets can be done in groups, helping each other

• They are gathered and recorded, but only lightly graded

Why Data Structures?

• The study of data structures is considered the cornerstone and starting point for systematic examination of computer science

• Notice I said computer science, not just programming

Ubiquitous

• Certain collection classes are found in almost every nontrivial program

• Knowing these keeps you from having to reinvent the wheel

Vocabulary

• The common data structures have well known names

• Helps provide a vocabulary that is shared by all computer professionals

Abstraction

• Data structures are one of the easiest ideas to visualize abstractly

• Abstraction is the key idea used by computer professionals to control complexity.

Information Hiding and Large Projects

• An important part of abstraction is information hiding - what details do you NOT need to know?

• Key to modern large software projects

• What does programmer A NOT need to know to use the work of programmer B?

Tools and Techniques

• The analysis of data structures provides a good vehicle to learn a variety of mathematical and other analytical techniques

• Recursion, big-Oh, counting arguments, proof of correctness

Design Pattern format

• A very useful and recent tool

• Design patterns provide a way to document proven solutions to common problems

• More importantly, provide a vocabulary

• Grew out of OO world, starting to become common everywhere else

Features of Design pattern

• Problem: Short statement of problem

• Solution: Abstract characteristics

• Forces: Reasons for using pattern

• Counter-Forces: Reasons for NOT using pattern

• Example - Façade (change in interface, not in implementation)

Questions??

• If not, then we do our first worksheet

What is O( )

int countOccurrences (double data [ ], int n, double testValue)

{ int i; int count = 0; for (i = 0; i < n; i++) { if (data[i] == testValue) count++; } return count;}

Btw, arrays and pointers are the same. More common:

int countOccurrences (double * data, int n, double testValue)

{ int i; int count = 0; for (i = 0; i < n; i++) { if (data[i] == testValue) count++; } return count;}

A loop that is not an array index. What is O( )?

int isPrime (int n) {

int i;

for (i = 2; i * i <= n; i++) {

if (0 == n % i) return 0;

}

return 1; /* 1 is true */

}

Calling a function inside another O( )?

void printPrimes (int n) {

int i;

for (i = 2; i < n; i++) {

if (isPrime(i))

printf(“Value %d is prime”, i); }

}

What is printf

• And older way to do printing (before streams).

• Don’t remember how to use it? Look in the handy pocket reference.

Nexted loop (Note, this is pseudo-code, not C)

void matMult (int [][] a, int [][] b, int [][] c) { int n = n; // assume all same size int i, j, k; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { c[i][j] = 0; for (k = 0; k < n; k++) c[i][j] += a[i][k] * b[k][j]; }}

A more subtle loop

void selectionSort (double * storage, int n) { int p, i, indexLargest; for (p = n – 1; p > 0; p--) { indexLargest = 0; for (i = 1; i <= p; i++) { if (storage[i] > storage[indexLargest]) indexLargest = i; } if (indexlargest != position) swap(storage, indexLargest, position); } }}

What is the sum?

• What is the sum of

1 + 2 + 3 + … + (n - 1) ?

An operator you might have never seen

for (i = n; i > 0; i = i >> 1) …

What is the >> operator? Right shift. Imagine that n starts at 100. What are the values that i will go through?