diff options
Diffstat (limited to 'csci1913')
-rw-r--r-- | csci1913/Java/project2_strap012.java | 68 |
1 files changed, 16 insertions, 52 deletions
diff --git a/csci1913/Java/project2_strap012.java b/csci1913/Java/project2_strap012.java index 4010657..bc50856 100644 --- a/csci1913/Java/project2_strap012.java +++ b/csci1913/Java/project2_strap012.java @@ -54,69 +54,33 @@ class Sort { // NUMBER slots, without making new NODEs. private static Node sortNodes(Node unsorted) { - Node left=null, right=null, sorted=null; - boolean firstSort=true; + Node left = null, right = null, sorted = null; if (unsorted==null || unsorted.next==null) { //unsorted list is either empty or of length 1 return unsorted; } else { - //--HALVING PHASE-- - int oddeven = 0; - boolean firstNodeR = true, firstNodeL = true; - while (unsorted!=null) { - - if (oddeven%2==0) { - if (firstNodeL) { - left=unsorted; - firstNodeL=false; + //Halving phase + int step = 1; + while (unsorted != null) { + // Node tL = left, tR = right; + if (step % 2 == 0) { //Odd case + if (right == null) { + right = unsorted; } else { - unsorted=left.next; + right.next=unsorted; } - } else { - if (firstNodeR) { - right=unsorted; - firstNodeR=false; + } else { //Even case + if (right == null) { + left = unsorted; } else { - unsorted = right.next; + left.next=unsorted; } } - oddeven++; - unsorted=unsorted.next; - } - } - //--SORTING PHASE-- - sortNodes(left); sortNodes(right); - //--COMBINING PHASE - while (left != null || right != null) { - if (left.number>right.number) { - if(firstSort) { - sorted = left; - firstSort = false; - } else { - sorted.next = left; - } - left = left.next; - } else { - if (firstSort) { - sorted = right; - firstSort = false; - } else { - sorted.next = right; - } - right = right.next; - } - } - if (left==null) { - while (right!=null) { - sorted.next=right; - right=right.next; - } - } else if (right==null) { - while (left!=null) { - sorted.next=left; - left=left.next; + unsorted = unsorted.next; + step++; } } + return sorted; } |