array_t
class to store random integer
values, e.g.,
#include "array.h"
array_t<int> A(atoi(argv[1]));
A.randseed();
A.randfill();
quicksort()
for the array_t
class.
main()
routine should then include:
int main()
{
// read in desired array size
// declare array variable A
...
// start the timer
A.quicksort(0,A.size()-1);
// stop the timer
// report the timer value
}
Note the passing in of left
and right
integer
indeces into quicksort()
. Recall that quicksort is
recursive and operates "in place" on the array, working over the
items within the [left,right]
array bounds.
quicksort.dat
data file that contains
two columns of numbers: the first column is n,
the size of the array, and the second column is t,
the time taken to sort the array.
bubblesort.dat
and
quicksort.dat
data files showing their
run times graphically against various other
theoretical bounds, e.g., O(n), O(log n),
O(n2).
gnuplot
to plot the resultant graph. Here is
a gnuplot script (call it plot.plt
) that plots
something reasonable:
reset
# this should work but need to add fontpath -- could be system-specific
set fontpath \
"/sw/share/texmf-dist/fonts/type1!" \
"/usr/share/texmf/fonts/type1!" \
"/usr/local/teTeX/share/texmf-dist/fonts/type1!" \
"/usr/share/texmf-texlive/fonts/type1!"
set term postscript eps enhanced "NimbusSanL-Regu" 22 \
fontfile "uhvr8a.pfb" fontfile "usyr.pfb" fontfile "utmri8a.pfb"
set xrange [*:*]
set xtics (0,50,500)
set yrange [-500:5000]
set key top right
set title "Runtime vs. Dataset Size"
set ylabel "Runtime (s)"
set xlabel "Dataset Size (n/1000)"
set output 'plot.eps'
plot \
x**2 title "O(n^2)" \
with lines lt 1 lw 3 lc rgb "#ff7f00", \
'bubblesort.dat' using ($1)/1000:($2)/1000 title "bubblesort" \
with linespoints pt 4 ps 2 lt 1 lw 3 lc rgb "#377eb8",\
'quicksort.dat' using ($1)/1000:($2)/1000 title "quicksort" \
with linespoints pt 4 ps 2 lt 1 lw 3 lc rgb "#e41a1c",\
x title "O(n)" \
with lines lt 1 lw 3 lc rgb "#4daf4a",\
log10(x)/log10(2) title "O(log_2 n)" \
with lines lt 1 lw 3 lc rgb "#984ea3"
Run the above script with the command
gnuplot plot.plt
.eps
file; convert this to something more useful,
like a PDF plot, via the command
epstopdf --nocompress -o=plot.pdf plot.eps
gnuplot
and epstopdf
commands
into a plot:
target in your Makefile
.
Then, rewrite your program to accept the size of array you want as
input argument argv[1]
and have it write out the
time to sort to std::cout
. This way, you can script
the run (and plot) within the Makefile
so that a
run
target executes the following:
./main 5 >> quicksort.dat
./main 500 >> quicksort.dat
./main 5000 >> quicksort.dat
./main 50000 >> quicksort.dat
./main 500000 >> quicksort.dat
then all you'll need to do to "automate" the entire process is type:
make main
make run
make plot
i
and j
).
Start i
at left
and
j
at right-1
.
array[i]
is less than the pivot,
increment i
.
j
is greater than (or equal to) the pivot,
decrement j
. At this point, element
i
and j
are both holding
elements that are out of position.
i
is still an index to the
left of j
. If it isn't, we're done
with partitioning. If it is, swap elements
i
and j
.
(Write a swap function.)
i
is
less than j
.
i
).
left
through i-1
.
i+1
through right
.
left + (right - left)/2
left
and
right
indeces specifying what section to sort.
tar.gz
archive of your lab02/ directory, including:
README
file containing
Makefile
.h
headers and .cpp
source)
make clean
before tar
)
sendlab
notes