# CSC 225 ASSIGNMENT 2 solution

\$24.99

Original Work ?

## Description

5/5 - (1 vote)

1 Programming Assignment
The programming assignment is to implement the merge sort algorithm with the following input and output speciﬁcation: Input: A linked list L containing n non-negative integers. Output: A linked list containing the elements of L in sorted (ascending) order. Pseudocode for merge sort is given below: 1: procedure MergeSort(L) 2: Split L into two lists L1,L2 of size (approximately) n/2 3: S1 ← MergeSort(L1) 4: S2 ← MergeSort(L2) 5: Merge the two sorted lists S1 and S2 together to a single sorted sequence S 6: return S 7: end procedure
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 structures1. All looping behavior must be implemented 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 implmentation inside the MergeSort function in the template. You may not change the
1. Including elements of the Java library which emulate loops.
1
name, return type or parameters of the MergeSort function. You may add additional functions as 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.
2 Test Datasets
Several ﬁles of test data have been uploaded to conneX. The Assignment 1 test data can be used as well.
The uploaded ﬁles may not cover all possible cases, so you should test your implementation on other inputs (particularly special cases, such as lists of size 0 or 1). One option is to write a short program to generate lists of integers for testing.
3 Evaluation Criteria
The programming assignment will be marked out of 30, based on a combination of automated testing (using large test arrays similar to the ones posted on conneX) and human inspection. Score (/30) Description 0−5 Submission does not compile or does not conform to the provided template. 6−10 The implementation uses arrays or data structures from the Java standard library. 11−15 The implemented algorithm is not merge sort or is substantially inaccurate on the tested inputs. 16−21 The implementation uses for, while or do-while loops for both the split and merge phases, but is otherwise correct and has a Θ(nlogn) running time. 22−26 The implemented algorithm is correct, has a Θ(nlogn) running time, and uses iterative loops for only one of the split and merge phases (and recursion for the other). 27−30 The implemented algorithm is correct, uses recursion for all looping behavior (and contains zero iterative loops), and has a Θ(nlogn) running time.
2