SlideShare a Scribd company logo
/* 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);
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()
{
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
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)
{
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
}
}
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;
}
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();
}
}

More Related Content

What's hot

Gevent what's the point
Gevent what's the pointGevent what's the point
Gevent what's the pointseanmcq
 
computer notes - Data Structures - 9
computer notes - Data Structures - 9computer notes - Data Structures - 9
computer notes - Data Structures - 9ecomputernotes
 
Effective Modern C++ - Item 35 & 36
Effective Modern C++ - Item 35 & 36Effective Modern C++ - Item 35 & 36
Effective Modern C++ - Item 35 & 36
Chih-Hsuan Kuo
 
Understanding greenlet
Understanding greenletUnderstanding greenlet
Understanding greenlet
Saúl Ibarra Corretgé
 
Using zone.js
Using zone.jsUsing zone.js
Using zone.js
Standa Opichal
 
Rust
RustRust
Stack switching for fun and profit
Stack switching for fun and profitStack switching for fun and profit
Stack switching for fun and profit
Saúl Ibarra Corretgé
 
Heap sort &amp; bubble sort
Heap sort &amp; bubble sortHeap sort &amp; bubble sort
Heap sort &amp; bubble sort
Shanmuga Raju
 
Ownership System in Rust
Ownership System in RustOwnership System in Rust
Ownership System in Rust
Chih-Hsuan Kuo
 
.NET Core Summer event 2019 in Brno, CZ - Async demystified -- Karel Zikmund
.NET Core Summer event 2019 in Brno, CZ - Async demystified -- Karel Zikmund.NET Core Summer event 2019 in Brno, CZ - Async demystified -- Karel Zikmund
.NET Core Summer event 2019 in Brno, CZ - Async demystified -- Karel Zikmund
Karel Zikmund
 
Singly Linked List
Singly Linked ListSingly Linked List
Singly Linked List
Er. Ganesh Ram Suwal
 
Exercice.docx
Exercice.docxExercice.docx
Exercice.docx
imane26
 
Performance testing of microservices in Action
Performance testing of microservices in ActionPerformance testing of microservices in Action
Performance testing of microservices in Action
Alexander Kachur
 
PyCon TW 2017 - PyPy's approach to construct domain-specific language runtime...
PyCon TW 2017 - PyPy's approach to construct domain-specific language runtime...PyCon TW 2017 - PyPy's approach to construct domain-specific language runtime...
PyCon TW 2017 - PyPy's approach to construct domain-specific language runtime...
Tsundere Chen
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4Abed Bukhari
 
Python meetup: coroutines, event loops, and non-blocking I/O
Python meetup: coroutines, event loops, and non-blocking I/OPython meetup: coroutines, event loops, and non-blocking I/O
Python meetup: coroutines, event loops, and non-blocking I/O
Buzzcapture
 
Apache PIG - User Defined Functions
Apache PIG - User Defined FunctionsApache PIG - User Defined Functions
Apache PIG - User Defined Functions
Christoph Bauer
 
Python for Scientists
Python for ScientistsPython for Scientists
Python for Scientists
Andreas Dewes
 
asyncio internals
asyncio internalsasyncio internals
asyncio internals
Saúl Ibarra Corretgé
 
Intro to Pig UDF
Intro to Pig UDFIntro to Pig UDF
Intro to Pig UDF
Chris Wilkes
 

What's hot (20)

Gevent what's the point
Gevent what's the pointGevent what's the point
Gevent what's the point
 
computer notes - Data Structures - 9
computer notes - Data Structures - 9computer notes - Data Structures - 9
computer notes - Data Structures - 9
 
Effective Modern C++ - Item 35 & 36
Effective Modern C++ - Item 35 & 36Effective Modern C++ - Item 35 & 36
Effective Modern C++ - Item 35 & 36
 
Understanding greenlet
Understanding greenletUnderstanding greenlet
Understanding greenlet
 
Using zone.js
Using zone.jsUsing zone.js
Using zone.js
 
Rust
RustRust
Rust
 
Stack switching for fun and profit
Stack switching for fun and profitStack switching for fun and profit
Stack switching for fun and profit
 
Heap sort &amp; bubble sort
Heap sort &amp; bubble sortHeap sort &amp; bubble sort
Heap sort &amp; bubble sort
 
Ownership System in Rust
Ownership System in RustOwnership System in Rust
Ownership System in Rust
 
.NET Core Summer event 2019 in Brno, CZ - Async demystified -- Karel Zikmund
.NET Core Summer event 2019 in Brno, CZ - Async demystified -- Karel Zikmund.NET Core Summer event 2019 in Brno, CZ - Async demystified -- Karel Zikmund
.NET Core Summer event 2019 in Brno, CZ - Async demystified -- Karel Zikmund
 
Singly Linked List
Singly Linked ListSingly Linked List
Singly Linked List
 
Exercice.docx
Exercice.docxExercice.docx
Exercice.docx
 
Performance testing of microservices in Action
Performance testing of microservices in ActionPerformance testing of microservices in Action
Performance testing of microservices in Action
 
PyCon TW 2017 - PyPy's approach to construct domain-specific language runtime...
PyCon TW 2017 - PyPy's approach to construct domain-specific language runtime...PyCon TW 2017 - PyPy's approach to construct domain-specific language runtime...
PyCon TW 2017 - PyPy's approach to construct domain-specific language runtime...
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4
 
Python meetup: coroutines, event loops, and non-blocking I/O
Python meetup: coroutines, event loops, and non-blocking I/OPython meetup: coroutines, event loops, and non-blocking I/O
Python meetup: coroutines, event loops, and non-blocking I/O
 
Apache PIG - User Defined Functions
Apache PIG - User Defined FunctionsApache PIG - User Defined Functions
Apache PIG - User Defined Functions
 
Python for Scientists
Python for ScientistsPython for Scientists
Python for Scientists
 
asyncio internals
asyncio internalsasyncio internals
asyncio internals
 
Intro to Pig UDF
Intro to Pig UDFIntro to Pig UDF
Intro to Pig UDF
 

Viewers also liked

Tutorial slideshare
Tutorial slideshareTutorial slideshare
Tutorial slideshare
mcarmerg
 
EKG-Generalidades
EKG-GeneralidadesEKG-Generalidades
EKG-Generalidades
William Pereda
 
Portifólio 2º semestre 2014
Portifólio 2º semestre 2014Portifólio 2º semestre 2014
Portifólio 2º semestre 2014
Sirlei Neto
 
Doc1
Doc1Doc1
Jowel Bodden Professional Persona Project
Jowel Bodden Professional Persona ProjectJowel Bodden Professional Persona Project
Jowel Bodden Professional Persona Project
jowelbodden
 
Gionny
GionnyGionny
Gionnyg1onny
 
Impresoras y post script
Impresoras y post scriptImpresoras y post script
Impresoras y post script
Claret Malmaceda Castillo
 
Google Objective-C Style Guide
Google Objective-C Style GuideGoogle Objective-C Style Guide
Google Objective-C Style Guide
Winston Hsieh
 
New Updates to Guidelines for Patients with Acute Ischemic Stroke
New Updates to Guidelines for Patients with Acute Ischemic StrokeNew Updates to Guidelines for Patients with Acute Ischemic Stroke
New Updates to Guidelines for Patients with Acute Ischemic Stroke
Dr Vipul Gupta
 
Cacbuocthuchientrochoi
CacbuocthuchientrochoiCacbuocthuchientrochoi
Cacbuocthuchientrochoi
Phamthithi1994
 
Ict phase 1 english
Ict phase 1   englishIct phase 1   english
Ict phase 1 english
sparky32
 

Viewers also liked (13)

3
33
3
 
Tutorial slideshare
Tutorial slideshareTutorial slideshare
Tutorial slideshare
 
EKG-Generalidades
EKG-GeneralidadesEKG-Generalidades
EKG-Generalidades
 
Portifólio 2º semestre 2014
Portifólio 2º semestre 2014Portifólio 2º semestre 2014
Portifólio 2º semestre 2014
 
Doc1
Doc1Doc1
Doc1
 
Jowel Bodden Professional Persona Project
Jowel Bodden Professional Persona ProjectJowel Bodden Professional Persona Project
Jowel Bodden Professional Persona Project
 
Biografía
BiografíaBiografía
Biografía
 
Gionny
GionnyGionny
Gionny
 
Impresoras y post script
Impresoras y post scriptImpresoras y post script
Impresoras y post script
 
Google Objective-C Style Guide
Google Objective-C Style GuideGoogle Objective-C Style Guide
Google Objective-C Style Guide
 
New Updates to Guidelines for Patients with Acute Ischemic Stroke
New Updates to Guidelines for Patients with Acute Ischemic StrokeNew Updates to Guidelines for Patients with Acute Ischemic Stroke
New Updates to Guidelines for Patients with Acute Ischemic Stroke
 
Cacbuocthuchientrochoi
CacbuocthuchientrochoiCacbuocthuchientrochoi
Cacbuocthuchientrochoi
 
Ict phase 1 english
Ict phase 1   englishIct phase 1   english
Ict phase 1 english
 

Similar to Sorter

#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
 #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
angelsfashion1
 
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdfJAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
fantasiatheoutofthef
 
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYMalikireddy Bramhananda Reddy
 
DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++
mustkeem khan
 
DSU C&C++ Practical File Diploma
DSU C&C++ Practical File DiplomaDSU C&C++ Practical File Diploma
DSU C&C++ Practical File Diploma
mustkeem khan
 
Linked lists
Linked listsLinked lists
Linked lists
George Scott IV
 
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdfImplement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
kostikjaylonshaewe47
 
import javautilQueue import javautilLinkedList import .pdf
import javautilQueue import javautilLinkedList import .pdfimport javautilQueue import javautilLinkedList import .pdf
import javautilQueue import javautilLinkedList import .pdf
ADITIEYEWEAR
 
Rust With async / .await
Rust With async / .awaitRust With async / .await
Rust With async / .await
Mario Alexandro Santini
 
JVM Mechanics: Understanding the JIT's Tricks
JVM Mechanics: Understanding the JIT's TricksJVM Mechanics: Understanding the JIT's Tricks
JVM Mechanics: Understanding the JIT's Tricks
Doug Hawkins
 
mainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdfmainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdf
fathimafancyjeweller
 
GeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsGeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good Tests
Tomek Kaczanowski
 
Confitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good TestsConfitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good Tests
Tomek Kaczanowski
 
includestdio.h #includestdlib.h int enqueue(struct node ,.pdf
includestdio.h #includestdlib.h int enqueue(struct node ,.pdfincludestdio.h #includestdlib.h int enqueue(struct node ,.pdf
includestdio.h #includestdlib.h int enqueue(struct node ,.pdf
galagirishp
 
These are the 4 functions #include KiostreamP using namespac.pdf
These are the 4 functions #include KiostreamP using namespac.pdfThese are the 4 functions #include KiostreamP using namespac.pdf
These are the 4 functions #include KiostreamP using namespac.pdf
JUSTSTYLISH3B2MOHALI
 
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdfHow do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
mail931892
 
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdfUsing NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
siennatimbok52331
 
OrderTest.javapublic class OrderTest {       Get an arra.pdf
OrderTest.javapublic class OrderTest {         Get an arra.pdfOrderTest.javapublic class OrderTest {         Get an arra.pdf
OrderTest.javapublic class OrderTest {       Get an arra.pdf
akkhan101
 
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdfPROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
climatecontrolsv
 
CODE#include stdlib.h #include stdio.hstruct task{proce.pdf
CODE#include stdlib.h #include stdio.hstruct task{proce.pdfCODE#include stdlib.h #include stdio.hstruct task{proce.pdf
CODE#include stdlib.h #include stdio.hstruct task{proce.pdf
fathimalinks
 

Similar to Sorter (20)

#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
 #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
 
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdfJAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
 
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
 
DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++
 
DSU C&C++ Practical File Diploma
DSU C&C++ Practical File DiplomaDSU C&C++ Practical File Diploma
DSU C&C++ Practical File Diploma
 
Linked lists
Linked listsLinked lists
Linked lists
 
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdfImplement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
 
import javautilQueue import javautilLinkedList import .pdf
import javautilQueue import javautilLinkedList import .pdfimport javautilQueue import javautilLinkedList import .pdf
import javautilQueue import javautilLinkedList import .pdf
 
Rust With async / .await
Rust With async / .awaitRust With async / .await
Rust With async / .await
 
JVM Mechanics: Understanding the JIT's Tricks
JVM Mechanics: Understanding the JIT's TricksJVM Mechanics: Understanding the JIT's Tricks
JVM Mechanics: Understanding the JIT's Tricks
 
mainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdfmainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdf
 
GeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsGeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good Tests
 
Confitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good TestsConfitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good Tests
 
includestdio.h #includestdlib.h int enqueue(struct node ,.pdf
includestdio.h #includestdlib.h int enqueue(struct node ,.pdfincludestdio.h #includestdlib.h int enqueue(struct node ,.pdf
includestdio.h #includestdlib.h int enqueue(struct node ,.pdf
 
These are the 4 functions #include KiostreamP using namespac.pdf
These are the 4 functions #include KiostreamP using namespac.pdfThese are the 4 functions #include KiostreamP using namespac.pdf
These are the 4 functions #include KiostreamP using namespac.pdf
 
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdfHow do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
 
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdfUsing NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
 
OrderTest.javapublic class OrderTest {       Get an arra.pdf
OrderTest.javapublic class OrderTest {         Get an arra.pdfOrderTest.javapublic class OrderTest {         Get an arra.pdf
OrderTest.javapublic class OrderTest {       Get an arra.pdf
 
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdfPROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
 
CODE#include stdlib.h #include stdio.hstruct task{proce.pdf
CODE#include stdlib.h #include stdio.hstruct task{proce.pdfCODE#include stdlib.h #include stdio.hstruct task{proce.pdf
CODE#include stdlib.h #include stdio.hstruct task{proce.pdf
 

More from Thomas Knudstrup (8)

NinjaSynch
NinjaSynchNinjaSynch
NinjaSynch
 
GreyCount
GreyCountGreyCount
GreyCount
 
CLinkedList
CLinkedListCLinkedList
CLinkedList
 
Generics
GenericsGenerics
Generics
 
distill
distilldistill
distill
 
assembly
assemblyassembly
assembly
 
Fibonnaci
FibonnaciFibonnaci
Fibonnaci
 
HappyFeat
HappyFeatHappyFeat
HappyFeat
 

Sorter

  • 1. /* 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(); } }