COMP 171 Data Structures and Algorithms
2002 Fall Term, MWF 12:00-12:50, Rm 2503
Dr. Dekai WU, Rm 3539, 2358-6989, dekai@cs.ust.hk
Programming Assignment 1

In this programming assignment you will implement a number of different sorting algorithms, and experimentally compare their performance. You will perform timing experiments to measure the run time. In addition, you will record the number of comparison operations and data movement operations required by each algorithm.

You will have to hand in a physical printout of each of your programs along with running time statistics. We will also electronically check your programs.

As usual, please remember to submit your work before the deadline. If you have any questions about this assignment, please send emails to shenyh@ust.hk.

DUE DATE

The deadline of the assignment is 12:00 noon, Wed 30 Oct 2002. Late assignments will not be accepted, since ACS will not collect them.

GRADING

Each function written should be adequately documented so that a reader will be able to understand what is being done. Your programs will be run on a standard test data set.

This assignment will account for 80 points in your final score. The general grading criteria for a program is as follows:

When your program does not run correctly on the test data, then your program will be graded by visual examination and the maximum score you can then get for Correctness is 30%.

ACS SUBMISSION

To avoid receiving zero credit for this assignment, please pay careful attention to the following instructions. We will electronically copy the programs from your account to ours for testing. To ensure that the copying routine performs properly you must do the following.

IMPLEMENTATION (60% of Correctness)

You are required to program four sorting functions, each with the same arguments:
  1. qsort(int keys[], int n) // quicksort
  2. hsort(int keys[], int n) // heapsort
  3. msort(int keys[], int n) // mergesort
  4. rsort(int keys[], int n) // radix sort

keys is an array of n elements containing the input list to be sorted. You may assume that each key is an integer 0 < k < 9999999. (Remember that in C++, an array argument only passes the pointer to the base address of the array, rather than the whole contents of the array.)

After calling the sort function, the elements in keys should be sorted. Note that for quicksort, heapsort, and radix sort, you may sort the list directly in the keys array (i.e., perform the sort in place). For mergesort, however, you will need to copy keys to a separate auxiliary array.

(Although you may want to keep these functions in separate files while implementing and testing, remember to store them all in the same file sortlib.cpp before turning in the assignment. Test them again after combining into one file, to make sure nothing has been accidentally broken!) Make sure there is no main() function in sortlib.cpp!

ANALYSIS (40% of Correctness)

You will measure the performance of each sorting algorithm in three ways.
  1. Run time --- you will time how long the algorithms take to run, using the UNIX time command. To get accurate measurements, you will need to call the same routine with the same data 10 or 100 or 1000 times, and then divide. Here's an example of how to use the time command:

    cssu31:dekai:158> time isortprog
    4.260u 0.060s 0:04.33 99.7% 0+105k 0+0io 0pf+0w

    The running time of the program is the user time + the system time, which in this example equals 4.26 + 060 = 4.32 seconds.

  2. Comparisons --- you will insert code in your functions to increment a counter every time a comparison is performed between any two elements of the data.
  3. Data movements --- you will insert code in your functions to increment a counter every time a data element is moved.

For each of these criteria, you should obtain measurements for n being 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, and 16384. Use the standard C random number generator rand() to produce unsorted lists (you will need to #include <stdlib.h>). For these experiments we are not interested in seeing the output.

Record the measurements in tables. Also create three graphs, one for each criterion, plotting the measurements for each type of sort against the input size to compare their performance.

TEST YOUR ALGORITHMS WITH THE TEST DRIVER

We will give you the following files to help you with your implementation and analysis:

The files containing the test arrays

We provide you with files containing arrays of different length to do your analysis. The length of an array is always an exponent of 2, ranging from 1024 to 524288. The files have the filename with the style "array****.txt" in which "****" represents the length of the array, i. e. the file "array1024.txt" contains an array with the length of 1024 and the file "array65536.txt" contains an array with the length of 65536.

"sortlib.hpp"

The file "sortlib.hpp" gives the name and parameters of the functions implementing the four required algorithms. You will use exactly the names and parameters for your functions defined in "sortlib.hpp".

"sort.cpp"

The file "sort.cpp" contains the "main" function you will use to test your sorting algorithms. It uses two arguments:

The "main" function works as follows:

How to do the test?

Please follow the following steps to go on with the test:

Calculate the running time

Use the command "time filename arg1 arg2" to calculate the running time of the program T1. Some factors affecting the final running time of your algorithm T:

RECAP

Write the four functions msort, qsort, hsort, and rsort and store them as directed in comp171/a1/sortlib.cpp in your CS UNIX account. Hand in hard copies of each of the programs along with tables and graphs describing the timing data collected.



Dekai WU, SHEN Yihai
dekai@cs.ust.hk, shenyh@ust.hk
Last update: 2002.10.15