-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble.js
More file actions
46 lines (43 loc) · 1.08 KB
/
Copy pathbubble.js
File metadata and controls
46 lines (43 loc) · 1.08 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
/* Bubble Sort */
var Arr,Util,
done = false,
stop = false;
var sleepTime;
async function BubbleSort(){
stop = false;
done = false;
while(!done && Arr){
done = true;
if(!Arr){return;}
for(let i = 0;Arr && i<Arr.length;i++){
if(Arr[i]>Arr[i+1]){
let last = Arr[i+1];
Arr[i+1] = Arr[i];
Arr[i] = last;
done = false;
}
}
await Util.sleep(sleepTime);
Arr ? postMessage(Arr) : null;
}
}
onmessage = async (arr) => {
if(typeof arr.data == "object"){
Arr = arr.data[0][0];
sleepTime = arr.data[0][1];
Util = arr.data[1];
console.log(arr.data);
console.log(Util);
var sorted = Util.isSorted(Arr);
if(!sorted){
let time = performance.now();
await BubbleSort();
let time2 = performance.now() - time;
console.log(time2/1000);
}else{
console.log("Array sorted already");
}
}else{
Arr = undefined;
}
}