Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions bubble-sort-int32-v2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
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 numberPool = 4096;
function initArray() {
const myArray = [];
// 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);
}
}
return new Int32Array(myArray);
};

function checkSort(myArray) {
for (let i = 0; i < myArray.length - 1; i++) {
if (myArray[i] > myArray[i + 1]) {
console.error(myArray[i], myArray[i + 1]);
break;
}
}
}

(function () {
const myArray = initArray();

initArray();

const startTime = process.hrtime.bigint();
bubbleSort(myArray);
const endTime = process.hrtime.bigint();

checkSort(myArray);

console.log(`[V8] array contains ${myArray.length} elements, execution time:`,
`${Number(endTime - startTime) / 1000000} ms`);
initArray();
})();

(function () {
const myArray = initArray().reverse();

const startTime = process.hrtime.bigint();
bubbleSort(myArray);
const endTime = process.hrtime.bigint();

checkSort(myArray);

console.log(`Reverse input: [V8] array contains ${myArray.length} elements, execution time:`,
`${Number(endTime - startTime) / 1000000} ms`);
})();
82 changes: 82 additions & 0 deletions bubble-sort-reverse-int32-v2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const bubbleSort = (a) => {
const len = a.length;

let sorted = false;
while (!sorted) {
sorted = true;
for (var i = a.length; --i;) {
let current = a[i];
let next = a[i - 1];

if (next > current) {
a[i] = next;
a[i - 1] = current;
sorted = false;
}
}
}
};

const numberPool = 4096;
function initArray() {
const myArray = [];
// 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);
}
}
return myArray;
};

function checkSort(myArray) {
for (let i = 0; i < myArray.length - 1; i++) {
if (myArray[i] > myArray[i + 1]) {
console.error(myArray[i], myArray[i + 1]);
break;
}
}
}

(function () {
const myArray = initArray();

initArray();

const startTime = process.hrtime.bigint();
bubbleSort(myArray);
const endTime = process.hrtime.bigint();

checkSort(myArray);

console.log(`[V8] array contains ${myArray.length} elements, execution time:`,
`${Number(endTime - startTime) / 1000000} ms`);
initArray();
})();

(function () {
const myArray = initArray().reverse();

const startTime = process.hrtime.bigint();
bubbleSort(myArray);
const endTime = process.hrtime.bigint();

checkSort(myArray);

console.log(`Reverse input: [V8] array contains ${myArray.length} elements, execution time:`,
`${Number(endTime - startTime) / 1000000} ms`);
})();
82 changes: 82 additions & 0 deletions bubble-sort-reverse-v2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const bubbleSort = (a) => {
const len = a.length;

let sorted = false;
while (!sorted) {
sorted = true;
for (var i = a.length; --i;) {
let current = a[i];
let next = a[i - 1];

if (next > current) {
a[i] = next;
a[i - 1] = current;
sorted = false;
}
}
}
};

const numberPool = 4096;
function initArray() {
const myArray = [];
// 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);
}
}
return myArray;
};

function checkSort(myArray) {
for (let i = 0; i < myArray.length - 1; i++) {
if (myArray[i] > myArray[i + 1]) {
console.error(myArray[i], myArray[i + 1]);
break;
}
}
}

(function () {
const myArray = initArray();

initArray();

const startTime = process.hrtime.bigint();
bubbleSort(myArray);
const endTime = process.hrtime.bigint();

checkSort(myArray);

console.log(`[V8] array contains ${myArray.length} elements, execution time:`,
`${Number(endTime - startTime) / 1000000} ms`);
initArray();
})();

(function () {
const myArray = initArray().reverse();

const startTime = process.hrtime.bigint();
bubbleSort(myArray);
const endTime = process.hrtime.bigint();

checkSort(myArray);

console.log(`Reverse input: [V8] array contains ${myArray.length} elements, execution time:`,
`${Number(endTime - startTime) / 1000000} ms`);
})();
80 changes: 55 additions & 25 deletions bubble-sort-v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,66 @@ const bubbleSort = (a) => {
}
};

const myArray = [];
const numberPool = 4096;
function initArray() {
const myArray = [];
// add numbers divisible by 2
for (let x = numberPool; x >= 0; x--) {
if (x % 2 === 0) {
myArray.push(x);
}
}

// 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 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);
}
}
return myArray;
};

// add numbers divisible by 7
for (let x = numberPool; x >= 0; x--) {
if (x % 7 === 0) {
myArray.push(x);
}
function checkSort(myArray) {
for (let i = 0; i < myArray.length - 1; i++) {
if (myArray[i] > myArray[i + 1]) {
console.error(myArray[i], myArray[i + 1]);
break;
}
}
}

console.log();
(function () {
const myArray = initArray();

initArray();

const startTime = process.hrtime.bigint();
bubbleSort(myArray);
const endTime = process.hrtime.bigint();

checkSort(myArray);

console.log(`[V8] array contains ${myArray.length} elements, execution time:`,
`${Number(endTime - startTime) / 1000000} ms`);
initArray();
})();

(function () {
const myArray = initArray().reverse();

const startTime = process.hrtime.bigint();
bubbleSort(myArray);
const endTime = process.hrtime.bigint();

const startTime = process.hrtime.bigint();
bubbleSort(myArray);
const endTime = process.hrtime.bigint();
checkSort(myArray);

console.log(`[V8] array contains ${myArray.length} elements, execution time:`,
`${Number(endTime - startTime) / 1000000} ms`);
// console.log(myArray);
console.log(`Reverse input: [V8] array contains ${myArray.length} elements, execution time:`,
`${Number(endTime - startTime) / 1000000} ms`);
})();