HASTY
JS
performance
Insertion sort VS. Merge sort
A performance test where we run Insertion sort against Merge sort.
Click the run button to get the performance of "insertion"- and "merge"-sorting algorithms.
Globals
const data = [ ...new Array(250).fill(0).map(Math.random) ]
Cases
0 ops/s
function insertionSort(arr) { const n = arr.length; for (let i = 1; i < n; i++) { const current = arr[i]; let j = i - 1; while (j > -1 && current < arr[j]) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = current; } return arr; } insertionSort(data);
0 ops/s
function mergeSort(arr){ const len = arr.length; if(len < 2) return arr; const mid = Math.floor(len/2); const left = arr.slice(0,mid); const right = arr.slice(mid); return merge(mergeSort(left),mergeSort(right)); } function merge(left, right){ const result = []; const lLen = left.length; const rLen = right.length; let l = 0; let r = 0; while(l < lLen && r < rLen){ if(left[l] < right[r]){ result.push(left[l++]); } else{ result.push(right[r++]); } } return result.concat(left.slice(l)).concat(right.slice(r)); } mergeSort(data);
Shorts
Wall of Love
Embed
Share
Donate
Options
Epochs:
Timeout:
Run