More Related Content More from Thomas Knudstrup More from Thomas Knudstrup (8) Sorter1. /* Copyright 2014 Thomas Knudstrup
*
* A Novel Sort Algorithm :
* Employing a merge methodology minimizing compares.
* Implemented generically utilizing recursion.
* With a JUnit test to stimulate and test the result
*
*/
package tom.example.code;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class Sorter {
int count = 0;
Node<String> root;
Attic<String> attic;
@Test
public void testSort()
{
int i = 1;
Node<String> newRoot = attic.sort();
while(newRoot.next != null)
{
System.out.println("" + i++ + ":" +newRoot.key);
2. assertTrue("Out of Order!",
(newRoot.compareTo(newRoot.next) <= 0) );
newRoot = newRoot.next;
}
System.out.println("" + i + ":" +newRoot.key);
assertTrue("Out of Order!", (i == count) );
}
@Before
public void setUp()
{
root = new Node<String>("peta"); count++;
Node<String> present = root.append("neta"); count++;
present = present.append("beta"); count++;
present = present.append("qeta"); count++;
present = present.append("aeta"); count++;
present = present.append("ueta"); count++;
present = present.append("zeta"); count++;
present = present.append("ceta"); count++;
present = present.append("feta"); count++;
present = present.append("yeta"); count++;
present = present.append("xeta"); count++;
present = present.append("geta"); count++;
present = present.append("keta"); count++;
attic = new Attic<String>(root);
System.out.println("setUp");
}
@After
public void tearDown()
{
3. System.out.println("tearDown");
}
}
class Attic<T extends Comparable<T>>
{
Node<T> root;
public Attic(Node<T> root) {
this.root = root;
}
public Node<T> sort()
{
Attic<T> attic = null;
Node<T> ptr = root;
while(ptr.next != null)
{
Node<T> snippy = ptr.next;
if(ptr.compareTo(snippy) > 0)
{
ptr.next = snippy.next;
if(snippy.compareTo(root) < 0)
{// prepend to present list
snippy.next = root;
root = snippy;
}
else
{// boot it to the attic
if(attic != null)
{// If attic already exists, prepend it
4. Node<T> temp = attic.getRoot();
snippy.next = temp;
attic.setRoot(snippy);
}
else
{// start new attic
snippy.next = null;
attic = new Attic<T>(snippy);
}
}
}
else
{// in order, no action
ptr = ptr.next;
}
}// !clean row while
//
if(attic != null)
{
Node<T> atticRoot = attic.sort();
root = merge(root,atticRoot);
}
// Merge attic
return root;
}
private Node<T> merge(Node<T> root, Node<T> atticRoot) {
Node<T> ptrOut = atticRoot;
while(ptrOut != null)
5. {
int compare = root.compareTo(ptrOut);
if(compare >= 0)
{
Node<T> snippy = ptrOut;
ptrOut = ptrOut.next;
snippy.next = root;
root = snippy;
}
else
{
Node<T> ptrIn = root;
while(ptrIn.next != null)
{
compare = ptrIn.next.compareTo(ptrOut);
if(compare < 0)
{
ptrIn = ptrIn.next;
}
else
{
Node<T> snippy = ptrOut;
ptrOut = ptrOut.next;
snippy.next = ptrIn.next;
ptrIn.next = snippy;
break; // exit inner
}
}
6. if(ptrIn.next == null)
{// got to the end in inner search
if(ptrOut != null)
{// before outer units exhausted, so just
append
ptrIn.next = ptrOut;
break; // and exit outer loop,
lists are merged
}
}
}
}
return root;
}
public Node<T> getRoot() {
return root;
}
public void setRoot(Node<T> root) {
this.root = root;
}
}
class Node<T extends Comparable<T>> implements Comparable<Node<T>>
{
static int compares;
T key;
Node<T> next;
Node(T key) {
this.key = key;
7. }
Node<T> append(T keyIn)
{
Node<T> newNode = new Node<T>(keyIn);
this.next = newNode;
return newNode;
}
@Override
public int compareTo(Node<T> o) {
compares++;
int value = key.compareTo(o.key);
return value;
}
@Override
public String toString()
{
return key.toString();
}
}