HASTY
JS
performance
Insertion sort VS. Quick sort
A performance test where we run Insertion sort against Quick sort.
Click the run button to get the performance of "insertion"- and "quick"-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 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