HASTY
JS
performance
Merge sort VS. Quick sort
A performance test where we run Merge sort against Quick sort.
Click the run button to get the performance of "merge"- and "quick"-sorting algorithms.
Globals
const data = [ ...new Array(250).fill(0).map(Math.random) ]
Cases
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);
0 ops/s
function quickSort(arr, left, right) { const len = arr.length; let pivot; let partitionIndex; if (left < right) { pivot = right; partitionIndex = partition(arr, pivot, left, right); quickSort(arr, left, partitionIndex - 1); quickSort(arr, partitionIndex + 1, right); } return arr; } function partition(arr, pivot, left, right) { const pivotValue = arr[pivot]; let partitionIndex = left; for (var i = left; i < right; i++) { if (arr[i] < pivotValue) { swap(arr, i, partitionIndex); partitionIndex++; } } swap(arr, right, partitionIndex); return partitionIndex; } function swap(arr, i, j) { const temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } quickSort(data);
Shorts
Wall of Love
Embed
Share
Donate
Options
Epochs:
Timeout:
Run