forked from idosela/algorithms-in-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adaptive-sort.js
184 lines (167 loc) · 6.29 KB
/
adaptive-sort.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
aij.adaptiveSort = (function(){
/*jshint bitwise: false*/
/*jshint noempty: false*/
"use strict";
/**
* Sorts an array of integers using the AdaptiveSort algorithm.
* @param {Array.<number>} items Array of items to be sorted.
*/
/*
* Adaptive merge sort algorithm
* Implementation: Eugene Scherba, 11/9/2010
*
* This is a stable sort algorithm, similar to merge sort except
* that it takes advantage of partially ordered "chains" (Donald
* Knuth refers to these structures as "runs"). Performance of this
* algorithm is directly dependent on the amount of preexisting
* partial ordering, however generally it is pretty good even on
* completely random arrays.
*
* Time complexity: O(n) if array is already sorted,
* O(n.log(n)) in a worst case which should be rare.
* Space complexity: O(n) in worst case, usually around O(n/2).
*/
function merge(left, right) {
/*
* Given two non-empty ordered arrays (chains), returns a new
* array containing an ordered union of the input chains.
*/
var left_len = left.length,
right_len = right.length,
left_val,
right_val,
result;
if (left[left_len - 1] <= (right_val = right[0])) {
result = left.concat(right);
} else if (right[right_len - 1] < (left_val = left[0])) {
result = right.concat(left);
} else {
/* By this point, we know that the left and the right
* arrays overlap by at least one element and simple
* concatenation will not suffice to merge them. */
result = new Array(left_len + right_len);
var k = 0, h = 0;
while (true) {
if (right_val < left_val) {
result[k + h] = right_val;
if (++h < right_len) {
right_val = right[h];
} else {
while (k < left_len) {
result[k + h] = left[k++];
}
break;
}
} else {
result[k + h] = left_val;
if (++k < left_len) {
left_val = left[k];
} else {
while (h < right_len) {
result[k + h] = right[h++];
}
break;
}
}
}
}
//setting array length to zero effectively removes the array from
//memory (older versions of Firefox would leak unless these arrays
//were reset).
left.length = 0;
right.length = 0;
return result;
}
function find_fchain(arr, offset, limit) {
/*
* Given an array and offset equal to indexOf(elA), find
* the (indexOf(elZ) + 1) of an element elZ in the array,
* such that all elements elA..elZ form a non-strict
* forward-ordered chain.
*/
var tmp, succ;
for (tmp = arr[offset];
++offset < limit && tmp <= (succ = arr[offset]);
tmp = succ
) {}
return offset;
}
function find_strict_rchain(arr, offset, limit) {
/*
* Given an array and offset equal to indexOf(elA), find
* the (indexOf(elZ) + 1) of an element elZ in the array,
* such that all elements elA..elZ form a strict
* reverse-ordered chain.
*/
var tmp, succ;
for (tmp = arr[offset];
++offset < limit && (succ = arr[offset]) < tmp;
tmp = succ
) {}
return offset;
}
function chain_unit(arr) {
// Step 1: return an array of chain arrays
// expecting data in reverse order
var terminus,
len = arr.length,
tmp = [],
f = find_strict_rchain;
for (var k = 0; k < len; k = terminus) {
// try to find a chain (ordered sequence of at least
// two elements) using a default function first:
terminus = f(arr, k, len);
if (terminus - k > 1) {
tmp.push(
(f === find_strict_rchain) ?
arr.slice(k, terminus).reverse() :
arr.slice(k, terminus)
);
} else if (f === find_strict_rchain) {
/* searched for a reverse chain and found none:
* switch default function to forward and look
* for a forward chain at k + 1: */
tmp.push(arr.slice(k, ++terminus));
f = find_fchain;
} else {
/* searched for a forward chain and found none:
* switch default function to reverse and look
* for a reverse chain at k + 1: */
tmp.push(arr.slice(k, ++terminus).reverse());
f = find_strict_rchain;
}
}
return tmp;
}
function chain_join(tmp) {
// Step 2: join all chains
var j = tmp.length;
if (j < 1) { return tmp; }
// note: we reduce the size of the array after each iteration,
// which is not really necessary (could be done at once at the end).
for (; j > 1; tmp.length = j) {
var k, lim = j - 2;
// At this point, lim == tmp.length - 2, so tmp[k + 1]
// is always defined for any k in [0, lim)
for (j = 0, k = 0; k < lim; k = j << 1) {
tmp[j++] = merge(tmp[k], tmp[k + 1]);
}
// Last pair is special -- its treatment depends on the initial
// parity of j, which is the same as the current parity of lim.
tmp[j++] = (k > lim) ? tmp[k] : merge(tmp[k], tmp[k + 1]);
}
var result = tmp.shift();
return result;
}
return function (arr) {
// immutable version -- store result in a separate location
return chain_join(chain_unit(arr));
// mutable (standard) version -- store result in-place
//var result = chain_join(chain_unit(arr));
//for (var k = 0, len = arr.length; k < len; k++) {
// arr[k] = result[k];
//}
//result.length = 0;
//return arr;
};
})();