Heap sort VS. Quick sort
A performance test where we run Heap sort against Quick sort.
Click the run button to get the performance of "heap"- and "quick"-sorting algorithms.
Globals
const data = [ ...new Array(250).fill(0).map(Math.random) ]
Cases
0 ops/s
function heapSort(arr) { const len = arr.length; let end = len - 1; heapify(arr, len); while (end > 0) { swap(arr, end--, 0); siftDown(arr, 0, end); } return arr; } function heapify(arr, len) { let mid = Math.floor((len - 2) / 2); while (mid >= 0) { siftDown(arr, mid--, len - 1); } } function siftDown(arr, start, end) { let root = start; let child = root * 2 + 1; let toSwap = root; while (child <= end) { if (arr[toSwap] < arr[child]) { swap(arr, toSwap, child); } if (child + 1 <= end && arr[toSwap] < arr[child + 1]) { swap(arr, toSwap, child + 1); } if (toSwap != root) { swap(arr, root, toSwap); root = toSwap; } else { return; } toSwap = root; child = root * 2 + 1; } } function swap(arr, i, j) { const temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } heapSort(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);
Embed
Share
Options
Epochs:
Timeout:
Run
H
ASTY.DEV
JS
performance
Pre-defined
Notebooks
Blog
Donate
Wall of love 😻
Telegram
Mastodon
Twitter
Use HASTY
Try demo