Lab 8 - Quicksort
Due: 5:00pm on Wednesday
The purpose of this lab is to investigate the quicksort algorithm. This lab
will also refresh one's skills at opening files and reading data from files.
Additionally, this lab serves as an introduction to the make
utility for those that have not taken CMPS 215.
Most of the code for this lab is provided for you in the
lab8 directory. Use the following command
to create a lab8 subdirectory off your current directory:
cp -r /home/fac/melissa/public_html/cs223-s10/lab8/ .
The -r
option to cp
is the recursive option. It will
create the lab8 subdirectory and copy all the files over to it.
File Descriptions
file_gen.cpp - This is a
variation on the file generator used for Homework 6. It generates data in the
range of 0 to 999. It also seeds random with the current time instead of a
static value. You compile file_gen.cpp with make file_gen
which
will make an executable called file_gen
vt100ansi.h - This header file
allows colors to be printed to the screen using the macro
vtprintf(COLOR)
where COLOR is one of the colors defined in the
file.
quicksort.cpp - This defines
the quicksort algorithm using median-of-three pivots. This version of
quicksort puts the pivot in the first slot, instead of the last slot as
discussed in class. Both approaches are valid. The code will call insertion
sort when the number of elements is less than the defined THRESHOLD value. You
can run quicksort in debug mode by defining DEBUG_MODE at the top of the file.
In debug mode, information about choosing the pivot, making the swaps and
doing insertion sort are printed to the screen. Since there is a lot of
information, SLEEP_USEC defines how long the debug mode pauses between stages
so that the information can be read. You can turn this pausing off by defining
SLEEP_USEC as 0 at the top of the file.
lab8main.cpp - This is the main
program. You will need to fill in a little code to open the specified file and
read the data into the local array. (Note: You will need to do a similar task
to this in Homework 6 too). To compile lab8main.cpp after making the required
changes, type make main
. This will create an executable called
main
, which can be run with main input1
or any other
filename.
input1,
input2 and
input3 - Pre-generated input files
containing 40 integers each.
Makefile - A makefile to compile
the above code into executables. This is needed for the make
commands to work.
Make utility
The make
utility and its associated Makefile
is a
way to specify program dependencies, particularly when one is using seperate
compilation. The Makefile defines all the associations in the form of:
target: dependency list
compilation command
You then tell make
to compile certain targets by giving the
command make <target>
.
Misc. Notes
This works best if you are using a color terminal with scrollback capacity.
If you are using your laptop instead of the machines in 311, make sure that
your terminal supports VT100 ANSI colors. Putty for Windows should work, as
should the Mac OS X terminal. The pausing code may behave oddly if you try to
download it and compile it on Windows (even though there is a workaround in
the code, Windows does poorly with this sort of task), so make sure to log
into Sleipnir and try the code there.
Assignment
Fill in the required portion of lab8main.cpp which involves opening a file
for reading and reading in the integers. Once you have added the file I/O
code, compile the program with make main
. Test different file
sizes and see how quicksort partitions the lists. Notice how even when the
list is not partitioned in the middle, the split operation still runs quickly.
Turn off debugging mode by removing, commenting out or undefining the
DEBUG_MODE line and recompile the program with make main
. Try
running the following:
time main input1
This will show you how long it takes to run the quicksort executable for
input1. You can try this for all the files you've generated for this lab.
However, Sleipnir is pretty fast, so you may not notice much of a time
difference with the defined MAX_CAPACITY.
Email me your modified lab8main.cpp.