-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertionSorter.java
More file actions
36 lines (33 loc) · 1.29 KB
/
InsertionSorter.java
File metadata and controls
36 lines (33 loc) · 1.29 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
package list;
public class InsertionSorter {
public void sort(DoublyLinkedList data) {
DoublyLinkedNode node = data.getHead();
while (node != null) { // This is the outer loop where node represents the node pointed by the black pointer in the video
DoublyLinkedNode prevNode = node.getPreviousNode();
while (prevNode != null && (prevNode.getData() > node.getData())) {
prevNode = prevNode.getPreviousNode();
}
// At this stage prevNode represents the node pointed by the green pointer (in video lecture)
DoublyLinkedNode next = node.getNextNode(); // we keep a reference to the next node of the black pointer for easier access
if (prevNode != null || !data.isHead(node)) {
node.getPreviousNode().setNextNode(next);
if (next != null) {
next.setPreviousNode(node.getPreviousNode());
}
node.setPreviousNode(prevNode);
}
if (prevNode == null) { // set the node as head
if (!data.isHead(node)) {
node.setNextNode(data.getHead());
node.getNextNode().setPreviousNode(node);
data.setHead(node);
}
} else { // of course the following connections can be made only when the prevNode is not null
node.setNextNode(prevNode.getNextNode());
prevNode.getNextNode().setPreviousNode(node);
prevNode.setNextNode(node);
}
node = next;
}
}
}