forked from arya2004/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
44 lines (37 loc) · 906 Bytes
/
Copy pathmain.js
File metadata and controls
44 lines (37 loc) · 906 Bytes
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
// Function to generate a random permutation of numbers 1..63
function randomVector() {
const v = [];
for (let i = 1; i < 64; i++) {
v.push(i);
}
// Shuffle the array using Fisher–Yates algorithm
for (let i = v.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[v[i], v[j]] = [v[j], v[i]];
}
return v;
}
// Function to calculate maxUpdates
function maxUpdates(arr) {
let count = 0;
let max = arr[0];
for (let i = 0; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
count++;
}
}
return count;
}
// Main simulation
async function main() {
const itr = 100000;
let cnt = 0;
for (let i = 0; i < itr; i++) {
const v = randomVector();
const c = maxUpdates(v);
cnt += c;
}
console.log("Average:", cnt / itr);
}
main();