-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbubble-sort-v2.js
executable file
·52 lines (43 loc) · 973 Bytes
/
bubble-sort-v2.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
const bubbleSort = (a) => {
const len = a.length;
let sorted = false;
while(!sorted) {
sorted = true;
for (let i = 0; i < len; i++) {
let current = a[i];
let next = a[i + 1];
if(next < current) {
a[i] = next;
a[i + 1] = current;
sorted = false;
}
}
}
};
const myArray = [];
const numberPool = 4096;
// add numbers divisible by 2
for (let x = numberPool; x >= 0; x--) {
if (x % 2 === 0) {
myArray.push(x);
}
}
// add numbers divisible by 3
for (let x = numberPool; x >= 0; x--) {
if (x % 3 === 0) {
myArray.push(x);
}
}
// add numbers divisible by 7
for (let x = numberPool; x >= 0; x--) {
if (x % 7 === 0) {
myArray.push(x);
}
}
console.log();
const startTime = process.hrtime.bigint();
bubbleSort(myArray);
const endTime = process.hrtime.bigint();
console.log(`[V8] array contains ${myArray.length} elements, execution time:`,
`${Number(endTime - startTime) / 1000000} ms`);
// console.log(myArray);