You are not allowed to use any of the standard Java collection types for this assignment. Do not import any Java standard library features from java.util.
For this assignment you are given the following Java source code files:
You must complete the public class named MySorts.java with methods as defined below.
Note that your *merge sort algorithm must be implemented to operate on IList objects, as defined in the given files iList.java and MyArrayList.java.
Structure of the MySorts Fields
Your merge sort algorithm should require no static (or instance) fields in your MySorts class.
Structure of the MySorts merge sort Methods
Your MySorts class must implement the following methods:
You may implement any additional methods that you may need.
MySorts merge sort algorithm
1. the mergesort method will take a reference to an IList< Integer > object as its only argument. This method will call the mergesortHelper method to do the actual work of sorting the list. This is the method that one would call to apply the quicksort algorithm to a list of values. By the time this method returns, the values in the argument IList should be sorted.
2. the mergesortHelper method is a recursive method that will implement the actual merge sort algorithm. The argument is a reference to the list of values that is to be sorted. this method will call the getLeftHalf and getRightHalf methods to divide a larger list, and call the merge method to merge two sorted sub-lists into one sorted list.
3. the getLeftHalf method will take as its argument, a reference to the list of values that is to be sorted. This method will return an IList< Integer > that contains the first half of the list that was passed to it as an argument.
4. the getRightHalf method will take as its argument, a reference to the list of values that is to be sorted. This method will return an IList< Integer > that contains the second half of the list that was passed to it as an argument.
5. the merge method will take two sorted lists as arguments. This method must create a new IList< Integer > and merge the two argument lists into one sorted list that will be returned.
IList.java
public interface IList< T extends Comparable< T > > {
/**
Adds an element at the end of the list.
*/
public void add(T item);
/**
Stores a new item at a specified index
Throws NoSuchElementException if index is out of bounds.
*/
public void set(int index, T item);
/**
Inserts an element at the specified index
Throws NoSuchElementException if index is out of bounds.
*/
public void insert(int index, T item);
/**
Removes the element at the specified index
Throws NoSuchElementException if index is out of bounds.
*/
public void remove(int index);
/**
Returns the element at the specified index
Throws NoSuchElementException if index is out of bounds.
*/
public T get(int index);
/**
Returns the size of the list.
@return the number of elements in the list
*/
public int size();
}
MyArrayList.java
import java.util.ArrayList;
public class MyArrayList< T extends Comparable< T > > implements IList< T > {
private ArrayList< T > list = new ArrayList< T >();
@Override
public void add(T item) {
list.add(item);
}
@Override
public int size() {
return list.size();
}
@Override
public T get(int index) {
return list.get(index);
}
@Override
public void set(int index, T item) {
list.set(index, item);
}
@Override
public void insert(int index, T item) {
list.add(index, item);
}
@Override
public void remove(int index) {
list.remove(index);
}
}