Programming Assignment 4

Monday, November 18


Statement of Work
Programming Assignment 4

1.0 Overview
  Ten different sorting algorithms were discussed in class. Of these, source code for a working example was shown for Bubble Sort, Insertion Sort, Selection Sort, QuickSort, MergeSort, ShellSort, ProxmapSort, and Radix Sort. In this programming assignment five of these algorithms will be implemented and compared for speed.
2.0 Requirements
  The student shall define, develop, document, prototype, test, and modify as required the software system.
  2.1 Requirements
  2.1.1 Using source code given below create a data file containing data for 10,000 structures. Each will contain a 5-digit integer key, a 4 character string, and a double.
  2.1.2 Create a program which implements 5 sorting algorithms. You must include Bubble Sort, and Proxmap Sort. In addition you must choose one from each of these three pairs: (1) Insertion Sort or Selection Sort, (2) Quick Sort or Shell Sort, (3) Merge Sort or Radix Sort.
  2.1.3 For each of the sort algorithms you will do the following: (1) Open the data file and read all data into an array which can hold 10,000 structures, (2) Read and store the start time, (3) Call a sorting algorithm, (4) Upon return from the sort function read and store the ending time, (5) Call a function which checks to make sure the array is now in sorted order, (6) Save the run time for this run of the sort algorithm, (7) Repeat steps 1-6 five times, and (8) Print the name of the sort method and the average time for the five runs for the algorithm on the screen.
3.0 Deliverables
  These products shall be delivered electronically via e-mail to the instructor.
3.1 Software Development Plan -- The student shall provide a printed software development plan for instructor approval NLT Tuesday, November 26.
3.2 Software Test Plan -- The student shall provide a printed test plan for complete verification and validation of the software for instructor approval NLT Tuesday, November 26.
3.3 Source Code -- The student shall provide electronic copies of the source code via e-mailed to the instructor. The source files shall be delivered not later than the start of the final exam. (11:30 AM, Friday, December 6)
3.4 Sorting Results -- The student shall provide a text document (.txt or MS Word .doc or .docx) summarizing the results of the sorting experiment. This final document shall be delivered via e-mail not later than the start of the final exam. (11:30 AM, Friday, December 6)
4.0 Period of Performance
  The period of performance of this assignment is 14 days from the date of assignment. Under NO circumstances will the assignment be accepted after the start of the final exam.

Hints

Data Structure and Data Array for Sorting

The downloadable data file has 15,000 lines each with the format:

12345 ABCD 99.9

where 12345 is the integer key for this record, ABCD is a 4-character string, and 99.9 is a randomly generated double value. In your program you should read only 10,000 records from the data file for this experiment. Note that some sorting algorithms, on some computers may crash the application if you try to use a file this large. This is especially true for those that use recursion. If this happens then reduce the number of records read by 100 until you get a size that will work. If your program runs OK with 10,000 you may want to try running with more than 10,000 records.

You will need to define a data structure similar to the one below to hold the data when it is read and create an array of these structures such as SortData dataArray[NUMRECS]; where NUMRECS has been #defined as the number of records to be read.
struct SortData
{
    int     key;
    char    strData[5];
    double  numData;
};

Sorting Algorithm Timer

Most computers are now so fast that using the normal time functions that are part of time.h to time some of the sort algorithms will result in a time of zero. This is because the time functions only provide times in milliseconds. Click on the link below to download a MicroSecondTimer class that you can add to your program 4 project which will be accurate down to microseconds. In your application you will need to create an instance of MicroSecondTimer...

MicroSecondTimer *mst = new MicroSecondTimer();

For each test you will need to first start the timer with

mst->Start();

Then run the sort function and immediately afterward stop the timer with

mst->Stop();

Now you can get the time by calling the getTime function getting the return value in a double variable.

timeInMS = mst->getTime();

Assume that timerInMS is a double. The time returned will be a double giving the time in seconds, but carried out to a fraction that will give a time in microsecond accuracy.

You will need to sum together the time results from five runs for each sorting algorithm implemented, then print out the average of those times. With something like...

cout << "SortName took " << averageTime << " microseconds." << endl;

In this output averageTime is a double.

Reading the Data File

Remember, you MUST read the data file EACH time before calling a sorting algorithm so you will start with the same conditions in testing the sort. Below are the functions you can use to read and store all data in the data array.
//-------------------------------------
// ReadData()
// Reads all data from data file into
// the data array
//-------------------------------------
bool ReadData(SortData DA[], int count)
{
	ifstream  inF;        // Input file stream
	char      line[64];   // Line read from file

	inF.open(DATAFILE, ifstream::in);
	if(!inF.is_open())
	{
		return false;
	}

	for(int i=0; i<count; i++)
	{
		getNextLine(&inF, line, 64);
		DA[i].key = atoi(strtok(line, " "));
		strcpy(DA[i].strData, strtok(NULL, " "));
		DA[i].numData = atof(strtok(NULL, " "));
	}

	inF.close();
	return true;
}

//-------------------------------------------
// GetNextLine()
// Read the next line from the file.
//-------------------------------------------
bool getNextLine(ifstream *inF, char *line, int lineLen)
{
    bool    done = false;

    while(!done)
    {
        inF->getline(line, lineLen);
        
        if(inF->good())    // If a line was successfully read
        {
           if(strlen(line) == 0)  // Skip any blank lines
                continue;
            else if(line[0] == '#')  // Skip any comment lines
                continue;
            else done = true;  // Got a valid data line so return it
        }
        else
        {
            strcpy(line, "");
            return false;  // Flag end of file
        }
    } // end while
	return true;
}




Click here to download the MicroSecondTimer class files, a data file for testing, and a .cpp file with main which you can use as the base for your program.

We will discuss this programming assignment in class.