$29
The assignment is to implement the merge sort algorithm with the following input and output speci cations:
Input: A linked list L of n non-negative integers.
Output: A linked list containing the elements of L in sorted order.
Pseudocode for merge sort is given below:
Algorithm 1 MergeSort(L)
Split L into two lists L1, L2 of size (approximately) n=2
S1 MergeSort(L1)
S2 MergeSort(L2)
Merge the two sorted lists S1 and S2 together to a single sorted sequence S return S
You may not use arrays of any kind, or any of the collection types provided by the Java standard library (such as ArrayList, LinkedList or Vector).
To receive full marks on this assignment, your code is not permitted to contain any for, while or do-while loops, or any other iterative looping structures. All looping behaviour must be im-plemented with recursion. A completely correct solution, using iterative loops for both the split phase (line 2 of the pseudocode above) and the merge phase (line 5 of the pseudocode above) will receive at most 70% of the available marks (see the `Evaluation Criteria' section below). If you prefer iterative code to recursive code, you may want to implement the algorithm iteratively rst, then re ne your solution to use recursion only.
Beyond the requirement that the implemented algorithm must be merge sort, there are no restrictions on how the split and merge operations behave. For example, you may nd that splitting the list by taking alternating elements (instead of dividing the list in half at the middle) is easier to implement with recursion.
A Java template has been provided containing an empty function MergeSort, which takes the head of a singly-linked list of integers as its only argument. Your task is to write the body of the MergeSort function. You must use the provided Java template as the basis of your submission, and put your implementation inside the MergeSort function in the template. You may not change the name, return type or parameters of the MergeSort function. You may add additional functions as
1
needed. A class called ListNode has been provided in the template to represent list nodes. Both the parameter value and return value of MergeSort will have type ListNode. You are not permitted to change any aspect of the ListNode class (including adding, removing, or renaming its contents). However, you are free to create a subclass of ListNode if you want to extend its functionality. Since you are are only permitted to submit one le, any extra classes must be contained in the MergeSort.java le.
The main function in the template contains code to help you test your implementation by entering test data or reading it from a le. You may modify the main function, but only the contents of the MergeSort function (and any functions you have added) will be marked, since the main function will be deleted before marking begins. Please read through the comments in the template le before starting.
Test Datasets
A set of input les containing test data are available on conneX. You should ensure that your implementation gives the correct answer on these test les before submitting. It may also be helpful to test your implementation on a variety of other inputs, since the posted data may not cover all possible cases.
Evaluation Criteria
The programming assignment will be marked out of 40, based on a combination of automated testing (using large test arrays similar to the ones posted on conneX) and human inspection.
Score
Description
0
- 7
Submission does not compile or does not conform to the provided
template.
8 - 14
The implementation uses arrays or data structures from the Java
standard library.
15
- 21
The implemented algorithm is not merge sort or is substantially
inaccurate on the tested inputs.
22
- 28
The implementation uses for, while, or do-while loops for both
the split and merge phases, but is otherwise correct and has a
(n log n) running time.
29
- 34
The implemented algorithm is correct, has a (n log n) running
time, and uses iterative loops for only one of the split and merge
phases (and recursion for the other).
35
- 40
The implemented algorithm is correct, uses recursion for all loop-
ing behaviour and has a (n log n) running time.
To be properly tested, every submission must compile correctly as submitted, and must be based on the provided template. If your submission does not compile for any reason (even trivial mistakes like typos), or was not based on the template, it will receive at most 10 out of 40. The best way to make sure your submission is correct is to download it from conneX after submitting and test it. You are not permitted to revise your submission after the due date, and late
2
submissions will not be accepted, so you should ensure that you have submitted the correct version of your code before the due date. conneX will allow you to change your submission before the due date if you notice a mistake. After submitting your assignment, conneX will automatically send you a con rmation email. If you do not receive such an email, your submission was not received. If you have problems with the submission process, send an email to the instructor before the due date.
3