CS2134
Fall 2007
Homework #1
Due Tuesday September 18, 11:59 p.m.
Your first assignment has a programming portion and a written portion. The
programming portion should consist of a single file called hw01.cpp, and the written
portion should be a single file called hw01written, in a format commonly understood as
indicated with its extension (for instance, .txt,
doc, .html, or .pdf). Be sure to include
your name and section at the beginning of each file!
You must hand both files via the digital drop box on MyPoly.
Programming Part:
In this program, you’ll measure the actual running time of three algorithms, to help you
understand the relationship between code structure, running time in bigOh notation,
and clock running time.
Given a vector a[] of integers, suppose we wish
to find the largest distance between
two elements, i.e., the largest value of a[i] – a[j], for any array indices
i and j.
Here are three algorithms for the problem:
1.
For each ordered pair (i,j), compute a[i] – a[j], keeping track of the maximum
difference found so far.
2.
For each ordered pair (i,j) with j >i , compute a[i] – a[j], keeping track of the
maximum difference found so far.
3.
Note that the maximum will be achieved when a[i] is the largest element in the
array and a[j] is the smallest. Scan the vector once from left to right, keeping
track of the largest element seen so far and the smallest seen so far. After
completing the scan, subtract the smallest from the largest.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentAlgorithm 1 explicitly considers all (i,j) pairs, thus has a O(n
2
) running time.
It will be up
to you to determine the running time of the other two algorithms.
Write functions MaxDiff1, MaxDiff2, and MaxDiff3, implementing the above algorithms.
Each function should take as an input parameter a vector of integers passed by const
reference, and return an integer (the maximum difference). If the vector is empty or has
a single element, the functions should return 0.
Read and understand the timer class posted in
timer.h
.
You will use the timer class to
measure the running time of the three MaxDiff functions on vectors of various sizes.
Ideally, we could measure the running time of function MaxDiff on a vector of size n as
follows:
1.
create a vector of size n, filled with integers of your choosing (Setting a[i] = i for
all i is OK, since the running time of the functions we’re considering only depends
on n, not on the order of the elements. For other problems it might be necessary
to fill the vectors more randomly.)
2.
reset the timer
3.
call MaxDiff, passing in the vector as an argument
4.
use the timer to find the elapsed time and report it.
Unfortunately, the granularity of the timer may be too large to measure the execution
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '07
 Hellerstein
 Algorithms, Scientific Notation, Data Structures, Ratio, #include

Click to edit the document details