Last active
August 19, 2018 08:44
-
-
Save memolog/9e9475a1b91770dd7b25d44904771fbb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function test(method, array, showArray){ | |
let arrayLength; | |
if (typeof array === 'number') { | |
arrayLength = array; | |
array = []; | |
for (let i=0; i<arrayLength; i++) { | |
array.push(Math.floor(Math.random()*10000)); | |
} | |
} else { | |
arrayLength = array.length; | |
} | |
const methods = method instanceof Array ? method : method.split(','); | |
methods.forEach((m)=>{ | |
const arrayCloned = [].concat(array); | |
console.log(`---- ${m.name || m} ----`); | |
if (showArray) { | |
console.log(arrayCloned); | |
} | |
console.time('sorting'); | |
m = typeof m === 'string' ? eval(m) : m; | |
eval(m)(arrayCloned, showArray); | |
console.timeEnd('sorting'); | |
}); | |
} | |
function bubbleSort(array, showArray) { | |
let count = 0; | |
const arrayLength = array.length; | |
let swapped; | |
for (let i=arrayLength; i>0; i--) { | |
swapped = 0; | |
for ( let j=1; j<i; j++ ) { | |
const a = array[j-1]; | |
const b = array[j]; | |
if (a > b) { | |
array[j-1] = b; | |
array[j] = a; | |
swapped = j; | |
} | |
count++; | |
} | |
if (!swapped) { | |
break; | |
} else { | |
i = swapped + 1; | |
} | |
if (showArray) { | |
console.log(array); | |
} | |
} | |
console.log(`number of iterates: ${count}`); | |
} | |
function selectionSort(array, showArray) { | |
let count = 0; | |
const arrayLength = array.length; | |
for (let j=0; j<arrayLength - 1; j++) { | |
let minIndex = j; | |
for (let i=j+1; i<arrayLength; i++) { | |
if (array[i] < array[minIndex]) { | |
minIndex = i; | |
} | |
count++; | |
} | |
if (j !== minIndex) { | |
const temp = array[j]; | |
array[j] = array[minIndex]; | |
array[minIndex] = temp; | |
} | |
if (showArray) { | |
console.log(array); | |
} | |
} | |
console.log(`number of iterates: ${count}`); | |
} | |
function insertionSort(array, showArray) { | |
let count = 0; | |
const arrayLength = array.length; | |
for (let i=1; i<arrayLength; i++) { | |
const a = array[i]; | |
let j = i-1; | |
for (; j>=0; j--) { | |
count++; | |
const b = array[j]; | |
if (a < b) { | |
array[j+1] = b | |
} else { | |
break; | |
} | |
} | |
array[j+1] = a; | |
if (showArray) { | |
console.log(array); | |
} | |
} | |
console.log(`number of iterates: ${count}`); | |
} | |
function topDownMerge(sourceArray, begin, middle, end, resultArray, showArray, count) { | |
let i = begin; | |
let j = middle; | |
for (let k = begin; k < end; k++) { | |
if (i < middle && (j >= end || sourceArray[i] <= sourceArray[j])) { | |
resultArray[k] = sourceArray[i]; | |
i = i + 1; | |
} else { | |
resultArray[k] = sourceArray[j]; | |
j = j + 1; | |
} | |
count.count++; | |
} | |
if (showArray) { | |
console.log(resultArray); | |
} | |
} | |
function topDownSplitMerge(sourceArray, begin, end, sortingArray, showArray, count) { | |
const length = end - begin; | |
if (length < 2) { | |
return; | |
} | |
const middle = Math.floor((end + begin) / 2); | |
topDownSplitMerge(sortingArray, begin, middle, sourceArray, showArray, count); | |
topDownSplitMerge(sortingArray, middle, end, sourceArray, showArray, count); | |
topDownMerge(sourceArray, begin, middle, end, sortingArray, showArray, count); | |
} | |
function topDownMergeSort(array, showArray) { | |
const count = { | |
count: 0 | |
}; | |
const workingArray = array.slice(); // copy array | |
topDownSplitMerge(workingArray, 0, array.length, array, showArray, count); | |
if (showArray) { | |
console.log(array); | |
} | |
console.log(`number of iterates: ${count.count}`); | |
} | |
const argv = process.argv; | |
test(argv[2], parseInt(argv[3], 10), argv[4]); |
Author
memolog
commented
Aug 7, 2018
node sortTest.js bubbleSort,selectionSort,insertionSort,topDownMergeSort 10000
---- bubbleSort ----
number of iterates: 49909120
sorting: 230.373ms
---- selectionSort ----
number of iterates: 49995000
sorting: 134.691ms
---- insertionSort ----
number of iterates: 24925812
sorting: 61.047ms
---- topDownMergeSort ----
number of iterates: 133616
sorting: 6.362ms
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment