Computer Science 112:

Introduction to Computer Science II

Gregory M. Kapfhammer

flickr photo shared by Billboard Art Project under a Creative Commons ( BY-NC-ND ) license

Color Scheme

Key Concept

Corresponding Diagram

In-Class Discussion

In-Class Activity

Details in the Textbook

Data Structure

Systematic way of organizing and accessing data

Algorithm

A step-by-step for performing a task in a finite time

Challenges

Comparable results only when using same environment

Limited set of inputs may lead to incorrect conclusions

You must fully implement and test the algorithms

Our Goals

Work independent of hardware and software

Takes into account all possible inputs

Does not require algorithm implementation

Primitive Operations

Assign a value to a variable

Compare two numbers

Return from a method

Call a method

Any questions about these types of operations?

Timings

Set your CLASSPATH to contain profiler.jar

Study the source code of this program!

Compile the ExampleTimings program

Perform a wide variety of experiments

Constant

Logarithm

Linear

n-log-n

Cubic

Exponential

Algorithm Growth Rates

log n: 3, 4, 5, 6, 7, 8, 9

n: 8, 16, 32, 64, 128, 256, 512

Quadratic: 64, 256, 1024, 4096, 16384, 65536, 262144

Cubic and exponential grow much faster!

Any questions about these growth rates?

Algorithm Choices

Fast algorithm running on a slow computer

Slow algorithm running on a fast computer

Algorithm Choices

Good algorithm design is very important

Ensure that your algorithm has a slow growth rate

Dramatic speedups in hardware cannot overcome asymptotically slow algorithms

See Tables 4.4 and 4.5 for more details!

arrayMax on page 171

This algorithm is O(n)!

Can you clearly explain why?

repeat1 on page 172

This algorithm is O(n2)!

Can you clearly explain why?

unique1 on page 174

This algorithm is O(n2)!

Can you clearly explain why?

unique2 on page 175

This algorithm is O(n×logn)!

Can you clearly explain why?