-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySort.java
More file actions
84 lines (72 loc) · 2.03 KB
/
MySort.java
File metadata and controls
84 lines (72 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
public class MySort {
public static <E extends Comparable<E>> void quickSort(MyNode<E> list) {
quickSort(list, getLastNode(list));
}
public static <E extends Comparable<E>> void quickSort(MyNode<E> first, MyNode<E> last) {
if(first != last && first != null) {
E pivot = first.element;
MyNode<E> current = first.next;
MyNode<E> previous = first;
while(previous != last && current != null) {
if (current.element.compareTo(pivot) < 0) {
previous.next = current.next;
first = current;
first.next = previous;
current = previous.next;
}
else {
previous = previous.next;
current = current.next;
}
//recursive calls will go here
}
}
}
public static <E extends Comparable<E>> MyNode<E> getMiddleNode(MyNode<E> head) {
MyNode<E> current = head;
MyNode<E> middle = head;
while(current.next.next != null) {
current = current.next.next;
middle = middle.next;
}
return middle;
}
public static <E extends Comparable<E>> MyNode<E> getLastNode(MyNode<E> head) {
MyNode<E> currentNode = head;
while(currentNode.next != null) {
currentNode = currentNode.next;
}
return currentNode;
}
/*public static <E extends Comparable<E>> void quickSort(MyNode<E> first, MyNode<E> last) {
if(first == last) {
return;
}
MyNode<E> pivot = last;
MyNode<E> current = first;
if(current.element.compareTo(pivot.element) > 0) {
MyNode<E> temp = new MyNode<E>(current.element);
current = current.next;
first = first.next;
last.next = temp;
}
while(current.next != pivot && current.next.next != null) {
if(current.next.element.compareTo(pivot.element) < 0) {
MyNode<E> temp = new MyNode<E>(current.next.element);
current.next = current.next.next;
temp.next = first;
first = temp;
}
else {
MyNode<E> temp = new MyNode<E>(current.next.element);
current.next = current.next.next;
last.next = temp;
last = temp;
}
current = current.next;
quickSort(first, pivot);
quickSort(pivot, last);
}
}
*/
}