-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgorithems.js
80 lines (65 loc) · 1.47 KB
/
Algorithems.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
var delay = parent.speed;
async function swap(arr, a, b) {
await sleep(delay);
let temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
if(parent.context != null){
//console.log(parent.context);
playfreq(a)
playfreq(b)
}
}
//the first sort so far
async function quickSort(arr, start, end) {
if (start >= end) {
return;
}
let index = await partition(arr, start, end);
//states[index] = -1;
await Promise.all([
quickSort(arr, start, index - 1),
quickSort(arr, index + 1, end)
]);
}
async function partition(arr, start, end) {
// for (let i = start; i < end; i++) {
// states[i] = 1;
// }
let pivotValue = arr[end].colNum;
let pivotIndex = start;
//states[pivotIndex] = 0;
for (let i = start; i < end; i++) {
if (arr[i].colNum < pivotValue) {
await swap(arr, i, pivotIndex);
//states[pivotIndex] = -1;
pivotIndex++;
//states[pivotIndex] = 0;
}
}
await swap(arr, pivotIndex, end);
// for (let i = start; i < end; i++) {
// if (i != pivotIndex) {
// states[i] = -1;
// }
// }
return pivotIndex;
}
// second sort
async function bubbleSort( arr, n)
{
var i, j;
for (i = 0; i < n-1; i++)
{
for (j = 0; j < n-i-1; j++)
{
if (arr[j].colNum > arr[j+1].colNum)
{
await swap(arr,j,j+1);
}
}
}
}
// radix sort
// merge sort
//