Sie sind auf Seite 1von 3

import java.util.

LinkedList;
/* COURSE : COP 3530
* Section : U1
* Semester : Fall 2011
* Instructor : Alex Pelin
* Author : Carlos Corvaia
* Assignment #: 4
* Due date : October 27, 2011
* Description : Write the constructor and the makeUnion method of the class
SortedLinkedList.
*
* I certify that the work is my own and did not consult with
* anyone.
*
*
* (sign)
*/
public class SortedLinkedList<T extends Comparable<? super T>> extends LinkedLis
t<T> {
private LinkedList<T> list; // the sorted list
private final static int MIN_LIST_SIZE = 10;
// sorts a linkedList depending on the size of the list
// quick sort if the size is greater than 10, otherwise we use Insertion sort
public SortedLinkedList(LinkedList<T> in) {
if (in == null) {
list = new LinkedList<T>();
}
list = new LinkedList<T>(in);//copies the sorted list "in" to "list"
quickSort(list, 0, in.size() - 1); //calls a helper method to sort the list
}
//a linkedList insetion sort *used for less than MIN_LIST_SIZE sorting
private void insertionSort(LinkedList<T> in, int low, int high) {
//checkes if the size of in is bigger than low + MIN_LIST_SIZE
if (in.size() > (low + MIN_LIST_SIZE)) {
quickSort(in, low, high); //runs a quick sort for bigger Linkedlist
}
for (int i = low + 1; i <= high; i++) {
T temp = in.get(i);
int j = i;
for (; j > 0 && temp.compareTo(in.get(j - 1)) < 0; j--) {
in.set(j, in.get(j - 1));
}
in.set(j, temp);
}
}
private void quickSort(LinkedList<T> in, int low, int high) {
// if list is less than 10 items use an insertion sort for faster sorting
if ((low + MIN_LIST_SIZE) > high) {
insertionSort(in, low, high);
} // else use a quicksort
else {
// sort mid, low, high for better pivot choosing
int mid = (low + high) / 2;
if (in.get(mid).compareTo(in.get(low)) < 0) //mid < low
{
swap(in, low, mid); // swap mid with low
}
if (in.get(high).compareTo(in.get(low)) < 0)//high < low
{

swap(in, high, low); // swap high low


}
if (in.get(high).compareTo(in.get(mid)) < 0)// high < mid
{
swap(in, high, mid);
}
//place pivot at position high -1 in the list
swap(in, mid, high - 1);
T pivot = in.get(high - 1);
//begin partitioning
int i, j;
for (i = low, j = high - 1;;) {
while (in.get(++i).compareTo(pivot) < 0)
;
while (pivot.compareTo(in.get(--j)) < 0)
;
if (i >= j) // if i passes j we break from the for loop
{
break;
}
swap(in, i, j);
}
// restore pivot
swap(in, i, high - 1);
System.out.println(in);
quickSort(in, low, i - 1);// call quicksort for smaller partition
quickSort(in, i + 1, high);//call quicksort for bigger partition
}
}
// swaps two elements in a linked list
private void swap(LinkedList<T> in, int a, int b) {
T temp = in.get(a); // saves content in index a in a temp Object
in.set(a, in.get(b)); // copy element in index b to index a
in.set(b, temp); // sets temp to index b
}
// return the union of the sorted linked lists this
// and other
public SortedLinkedList<T> makeUnion(SortedLinkedList<T> other) {
SortedLinkedList<T> temp = new SortedLinkedList<T>(new LinkedList<T>()); //temp
list to save the union
LinkedList<T> copyThis = new LinkedList<T>(list); // a copy of this LinkedList
LinkedList<T> copyOther = new LinkedList<T>(other.list); // a copy of the Other
linkedList
for (;;) {
if (copyThis.isEmpty()) {
temp.list.addAll(copyOther);
return temp;
}
if (copyOther.isEmpty()) {
temp.list.addAll(copyThis);
return temp;
}
// if the first element of list is less than the first element of other
if (copyThis.peek().compareTo(copyOther.peek()) <= 0) {
temp.list.add(copyThis.pop()); //pops list and adds it to temp
} // if the first element of list is greater than the first element of other
else if (copyThis.peek().compareTo(copyOther.peek()) > 0) {
temp.list.add(copyOther.pop()); // pops other and adds it to temp
}
}

//return temp;// you write the loop. Use the merge in mergeSort as an example.
}
// print the items in list
public void print() {
// if (list == null) {
// System.out.println("the list is null");
// } else {
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}