Project 2: Sorting lists

The goal of this project is to practice manipulating linked lists.

1. Introduction and setup

In this project you will implement insertion sort and merge sort for linked lists. I am providing infrastructure for this project similar to what I provided for the first two labs and project 1, except in Java and for lists instead of arrays.

After moving into your directory for this course, copy the given files for this project:

cp /homes/tvandrun/Public/cs245/proj3/* .

You should get the following files:

Node.java
IntListPair.java
SortUtil.java
SortList.java
SelectionL.java

The class SelectionL has a method with signature

    public static IntListPair sort(Node list) {

Given a list (or, the head node of a list), it wll perform a list version of selection sort, rearranging the nodes so they are sorted. It will then return an IntListPair, an object holding an integer and the head node of a list. The integer is used for counting comparisons. However, we will not be counting comparisons in this project---that part of the code is left over from a similar project I assigned in a previous semester. You can ignore it and always return 0 as the number of comparisons.

SortList has a main method. If you give it the name of a class with a list sorting algorithm on the command line, it will generate a random linked list (using the Node.java class) and run the given sorting algorithm on it. For example

SortList SelectionL

Will test out the algorithm in the SelectionL class. The only requirement is that the given class has a static method with the signature mentioned above.

2. Your task

Open two new files, InsertionL.java and MergeL.java, and write sort methods using the given signature in each class, implementing insertion sort and merge sort, respectively. (The classes may contain additional methods if you want; in particular, you may find it easier to have a helper method or two for merge sort.)

It is possible to create a sorted list by making new instances of the node class for the new, sorted list. Just to make this project more interesting, you may not instantiate the Node class. Instead, just rearrange the nodes you are given.

Note that unlike in class, there is no List class. Instead, lists are represented by the head node.

Test your code using SortList.

3. To turn in

In a typescript, cat the files you wrote and run the driver several times on each algorithm you wrote, to demonstrate that it is correct.

DUE: Thurs, Sept 30, 5:00 pm.


Thomas VanDrunen
Last modified: Wed Nov 3 17:01:35 CDT 2010