forked from iam-peekay/JavaScript-Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkerSort.js
More file actions
50 lines (49 loc) · 1.2 KB
/
WorkerSort.js
File metadata and controls
50 lines (49 loc) · 1.2 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
/**
* Created by Stefano on 01/04/14.
*/
onmessage = function (event) {
var data = event.data;
switch (data.cmd) {
case 'start':
this.finished = 0;
this.from = data.from;
this.to = data.to;
this.worker = data.worker;
if (this.from === this.to) {
this.postMessage({cmd: 'finished', worker: this.worker});
close();
}
break;
case 'finished':
this.finished++;
if (this.finished > 1) {
this.array = data.array;
merge(this.from, this.to, this.array);
this.postMessage({cmd: 'finished', worker: this.worker});
close();
}
break;
default :
this.postMessage('Something went wrong');
}
};
//noinspection FunctionWithMultipleLoopsJS
function merge(from, to, array) {
var m = Math.floor((from + to) / 2);
var left = [];
var right = [];
for (var i = 0; i < m - from + 1; i++)
left[i] = array[from + i];
for (var j = 0; j < to - m; j++)
right[j] = array[m + j + 1];
var x = 0, y = 0;
for (var k = from; k < to + 1; k++) {
if (y > to - m - 1 || (left[x] <= right[y] && x < m - from + 1)) {
this.postMessage({cmd: 'replace', index: k, value: left[x]});
x++;
} else {
this.postMessage({cmd: 'replace', index: k, value: right[y]});
y++;
}
}
}