From 19d945f9db2c94094d9ab0afb5ad8dd1c32bf39e Mon Sep 17 00:00:00 2001 From: RossTheRoss Date: Sun, 24 Nov 2019 13:53:42 -0600 Subject: Basically steal someone else's code lol --- csci1913/Java/project2_strap012.java | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) (limited to 'csci1913/Java/project2_strap012.java') diff --git a/csci1913/Java/project2_strap012.java b/csci1913/Java/project2_strap012.java index 69793f0..a102314 100644 --- a/csci1913/Java/project2_strap012.java +++ b/csci1913/Java/project2_strap012.java @@ -54,29 +54,31 @@ class Sort { // NUMBER slots, without making new NODEs. private static Node sortNodes(Node unsorted) { + Node left = null, right = null, sorted = null; if (unsorted==null || unsorted.next==null) { //unsorted is either empty or of length 1 return unsorted; } //Halving phase - Node left = null, right = null, sorted = null; - int step = 1; - while (unsorted != null) { - if (step % 2 == 0) { //Odd case - if (right == null) { + else { + int step = 1; + while (unsorted != null) { + Node next = unsorted.next; + if (step % 2 == 0) { //Even case + //Pops out first value and puts into Right + unsorted.next = right; right = unsorted; - } else { - right.next = unsorted; - } - } else { //Even case - if (left == null) { - left = unsorted; - } else { - left.next = unsorted; + } else { //Odd case + // Pops out first value and puts into Left + unsorted.next = left; + left = unsorted; } + step++; + unsorted = next; } - step++; - unsorted = unsorted.next; } + left = sortNodes(left); + right = sortNodes(right); + return sorted; } -- cgit v1.2.3