-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
90 lines (66 loc) · 1.97 KB
/
worker.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
function arrayGenerator(e) {
var arrayItems = []
for (var i = 0; i < e.data; i++) {
do {
randomNumber = Math.floor((Math.random() * e.data) + 1)
} while (arrayItems.indexOf(randomNumber) >= 0);
arrayItems.push(randomNumber)
}
return arrayItems
}
function swap(items, firstIndex, secondIndex){
var temp = items[firstIndex];
items[firstIndex] = items[secondIndex];
items[secondIndex] = temp;
}
function partition(items, left, right) {
var pivot = items[Math.floor((right + left) / 2)],
i = left,
j = right;
while (i <= j) {
while (items[i] < pivot) {
i++;
}
while (items[j] > pivot) {
j--;
}
if (i <= j) {
swap(items, i, j);
i++;
j--;
}
}
return i
}
function quickSort(items, left, right) {
var index
if (items.length > 1) {
left = typeof left != "number" ? 0 : left
right = typeof right != "number" ? items.length - 1 : right
index = partition(items, left, right)
if (left < index - 1) {
quickSort(items, left, index - 1)
}
if (index < right) {
quickSort(items, index, right)
}
}
return items
}
self.onmessage = function(numberOfItems) {
var start_arrayGenerator = new Date().getTime()
var originalRandomArray = arrayGenerator(numberOfItems)
var end_arrayGenerator = new Date().getTime()
var randomArrayTotalTime = end_arrayGenerator - start_arrayGenerator
var randomArray = originalRandomArray.slice(0)
var start_sortedArray = new Date().getTime()
var sortedArray = quickSort(originalRandomArray)
var end_sortedArray = new Date().getTime()
var sortedArrayTotalTime = end_sortedArray - start_sortedArray
postMessage({
'randomArray': randomArray,
'randomArrayTotalTime': randomArrayTotalTime,
'sortedArray': sortedArray,
'sortedArrayTotalTime': sortedArrayTotalTime
})
}