Last active
August 29, 2015 14:10
-
-
Save ljstrnadiii/415210ac73d5c768ce2a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MySort { | |
public static <E extends Comparable<E>> void quickSort(MyNode<E> list) { | |
MyNode<E> last = list; | |
while (last.next !=null && list !=null) { | |
//System.out.println("yes"); | |
last = last.next; | |
} | |
System.out.println(list); | |
sort(list, last); | |
} | |
public static <E extends Comparable<E>> void sort(MyNode<E> head, MyNode<E> last) { | |
MyNode<E> Pivot, lTail=null, rTail=null, temp, new_temp, left = null, right = null; | |
temp = head; | |
E pivot = head.element; | |
Pivot = head; | |
last.next = null; | |
//compare and place in left or right | |
while(temp.next != null){ | |
System.out.println("*"); | |
temp = temp.next; | |
if(temp.element.compareTo(pivot) <= 0) { | |
System.out.println("yess"); | |
if(left == null) { | |
left = temp; | |
lTail=temp ; | |
}else{ | |
new_temp = left; | |
left = temp; | |
left.next = new_temp; | |
} | |
} | |
if(temp.element.compareTo(pivot) > 0) { | |
System.out.println("noo"); | |
if(right == null) { | |
right = temp; | |
rTail=temp; | |
}else{ | |
new_temp = right; | |
right = temp; | |
right.next = new_temp; | |
} | |
} | |
} | |
//Connect left pivot and right and recursively call function | |
if(left != null && right != null){ | |
lTail.next=Pivot; | |
Pivot.next=right; | |
sort(left, lTail); | |
sort(right, rTail); | |
}else if (left == null & right != null){ | |
Pivot.next=right; | |
sort(right,rTail); | |
}else if (left != null & right == null){ | |
lTail.next=Pivot; | |
sort(left,lTail); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment