Assignment 6

Due date:

Late assignments will be penalized 10% per day, and will not be accepted after .

1. Hand In

Each student is required to do this assignment individually and to hand in the following: Place these items in a 9"x11" envelope, with the following information clearly marked on the outside of the envelope:

Name, Student Number, Assignment Number, Course Number (T26)

Assignments which are not in an envelope will not be marked

Deposit your envelope in .

Marking Scheme

Programming standards: 10pt, Question 1a: 18pt, Questions 1b, 1c and 1d: 8pt, Question 2: 18pt, Total: 70pt.

Your program must conform to the programming standards. These are described in a separate sheet.

2. Program Handout

In this assignment, you are given a complete implementation of the abstract type `list'. You must use the implementation to write sorting functions. These functions must be at the `application' level: that is, they must be written completely independently of the exact manner in which lists are implemented: they must do all their list manipulations using the abstract list operations defined in the list specification.

Note that the main program writes out information about how much memory has been allocated by your procedures but not returned to the global pool. These numbers should always be 0: if they are not, you have bugs to fix.

The program handout consists of 2 files: Partial6.c and Partial6.h. The only file you should modify is Partial6.c. Partial6.h is automatically included and supplies an appropriate testing procedure for questions 1a, 1c, 1d and 2 - there is no testing procedure for question 1b. In order to get appropriate testing procedure for the question you are working on, you must modify accordingly the 2nd macro definition at the top of file Partial6.c. For example, to get the testing procedure for question 1a, it should be:

        #define QUESTION1a
whereas, for question 2 it should be:
        #define QUESTION2

3. Question 1a

Write a procedure merge meeting this specification: The specification given above is slightly inconvenient to work with. Instead, we change the postconditions to be: Since we need to know which one is the result, we further require merge to be a function that returns this result as its value. Thus, after executing l1 = merge(l1,l2,Nc,Njs), l1 contains the result of the merge and the `other' list has been destroyed. In other words, at this point, (1) either l2 has been destroyed, or (2) the old l1 has been destroyed and now l1==l2.

4. Question 1b

Write a procedure general_sort that meets this specification: The procedure cut_in_two meets this specification:

5. Question 1c

Write a procedure merge_sort that implements the mergesort algorithm simply by calling general_sort with an appropriate cut_in_two procedure (which you must also write). merge_sort should return the sorted list L' and the integers Nc and Njs returned by general_sort.

6. Question 1d

Write a procedure insertion_sort that implements the insertion sort algorithm simply by calling general_sort with an appropriate cut_in_two procedure (which you must also write). insertion_sort should return the sorted list L' and the integers Nc and Njs returned by general_sort.

7. Question 2

Choose five different lengths of lists ranging from small (e.g. 10) to large (as large as you can and still finish executing in a few minutes), with reasonably-spaced intermediate values.

For each of these list lengths, run the program written for question 1 five times using a different random number seed each time. Average the results.

Plot the average number of comparisons and the average number of splits/joins for the two sorting algorithms (merge sort and insertion sort) as a function of the length of the list.

Justify your answers.