Lab 10 - Quicksort
The purpose of this lab is to investigate the quicksort algorithm.
Most of the code for this lab is provided for you in the
lab10 directory. Use the following command
to create a lab10 subdirectory off your current directory:
cp -r /home/fac/melissa/public_html/cs223/lab10/ .
The -r
option to cp
is the recursive option. It will
create the lab10 subdirectory and copy all the files over to it.
File Descriptions
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.
input1,
input2 and
input3 - Pre-generated input files
containing 40 integers each.
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 240, 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
onto Sleipnir and try the code there.
Assignment
Create a lab10.cpp file which includes the quicksort.cpp file. In main(), open
one of the provided input files (or take input from the keyboard). You may
either prompt the user for the input filename or take the input filename as a
command line argument. Read all of the numbers from the file (or keyboard)
into an array and pass that array to the quicksort function. Use 0 for the
starting index and count-1 for the ending index (this means you need to make
sure to keep track of the count of numbers you read off the file).
Once you have made your lab10.cpp file, compile the program and test the code
with different sets of input (Note: you can use the file_gen code from the
homework assignment to create new files with different data than the provided
files). Notice how even when the list is not partitioned in the middle, the
split operation still runs quickly.
Email Terry your lab10.cpp file. You do not need to email the other files as
they were provided.