-
Notifications
You must be signed in to change notification settings - Fork 0
/
linksort.js
82 lines (74 loc) · 1.69 KB
/
linksort.js
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
class Node {
constructor(data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
function insertLast(value, head, tail) {
let newNode = new Node(value);
if (tail !== null) {
tail.next = newNode;
}
newNode.prev = tail;
tail = newNode;
if (head === null) {
head = newNode;
}
return { head, tail };
}
function insertFirst(value, head, tail) {
let newNode = new Node(value);
newNode.prev = null;
if (head !== null) {
head.prev = newNode;
}
newNode.next = head;
head = newNode;
if (tail === null) {
tail = newNode;
}
return { head, tail };
}
function insertBetween(value, temp, tail) {
let newNode = new Node(value);
while (temp.next !== null && temp.next.data < value) {
temp = temp.next;
}
newNode.next = temp.next;
newNode.prev = temp;
if (temp.next !== null) {
temp.next.prev = newNode;
}
temp.next = newNode;
if (newNode.next === null) {
tail = newNode;
}
return tail;
}
function resetArray(A, n, temp) {
for (let i = 0; i < n; i++) {
A[i] = temp.data;
temp = temp.next;
}
}
function linkSort(A, n) {
let head = null;
let tail = null;
({ head, tail } = insertFirst(A[0], head, tail));
for (let i = 1; i < n; i++) {
if (A[i] <= head.data) {
({ head, tail } = insertFirst(A[i], head, tail));
} else if (A[i] >= tail.data) {
({ head, tail } = insertLast(A[i], head, tail));
} else {
tail = insertBetween(A[i], head, tail);
}
}
resetArray(A, n, head);
}
// Test the linkSort function
const X = [5, 3, 2, 10, 9, 0, -10, 8, 8, 1, 10, 30, 44, 31, 22];
const n = X.length;
linkSort(X, n);
console.log(X.join(' '));