Skip to content

Added a loop that runs the benchmark for different values of N #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
100 changes: 56 additions & 44 deletions src/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,54 +17,66 @@ const chance = new Chance();

// Helper Functions

function makeShuffledRange(len) {
const arr = [];
for (let i = 0; i <= len; i++) {
arr.push(i);
}
return arr.sort(() => Math.random() - 0.5);
}

function isSorted(arr) {
let sorted = true;
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i] > arr[i+1]) {
sorted = false;
break;
}
}
return sorted;
}

function runIt(algo) {
const cp = [...testData];
const start = performance.now();
const result = algo(cp);
results.push({ name: algo.name, time: performance.now() - start })
!isSorted(result) && console.log(algo.name, 'sorting failed!');
result.length !== testData.length && console.log(algo.name, 'length mismatch!');
}

// Main Testing

// const testData = makeShuffledRange(10000);
// const testData = makeRandomArrayOfChars(10000);
// const testData = chance.n(chance.character, 10000);
const testData = chance.n(chance.hammertime, 10000);
const results = [];


runIt(selectionSort);
runIt(bubbleSort);
runIt(quickSort);
runIt(insertionSort);
runIt(mergeSort);
runIt(heapSort);
runIt(cocktailShakerSort);
runIt(shellSort);
runIt(gnomeSort);
runIt(radixSort); // only works on ints
runIt(jsSort);
// runIt(bogoSort); // careful now
for (let numElements = 100; numElements < 10000000; numElements *= 10) {

console.table(results.sort((a, b) => a.time - b.time));
function makeShuffledRange(len) {
const arr = [];
for (let i = 0; i <= len; i++) {
arr.push(i);
}
return arr.sort(() => Math.random() - 0.5);
}

function isSorted(arr) {
let sorted = true;
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i] > arr[i+1]) {
sorted = false;
break;
}
}
return sorted;
}

function runIt(algo) {
const cp = [...testData];
const start = performance.now();
const result = algo(cp);
results.push({ name: algo.name, time: performance.now() - start })
!isSorted(result) && console.log(algo.name, 'sorting failed!');
result.length !== testData.length && console.log(algo.name, 'length mismatch!');
}

console.log(`\nGenerating test data for ${numElements} elements...`);
const testData = chance.n(chance.hammertime, numElements);
console.log('Running tests...');
const results = [];

// O(n)
runIt(radixSort); // only works on ints
// O(n log n)
runIt(jsSort);
runIt(mergeSort);
runIt(quickSort);
runIt(heapSort);
// Depends
runIt(shellSort);
// O(n^2)
runIt(bubbleSort);
runIt(cocktailShakerSort);
runIt(gnomeSort);
runIt(insertionSort);
runIt(selectionSort);


// runIt(bogoSort); // careful now

console.table(results.sort((a, b) => a.time - b.time));

}