Sorting Algorithms

Benchmark published by System on

Description

Classic sorting algorithms vs native sort.

Setup

const generateData = () => Array.from({length: 1000}, () => Math.random() * 1000);
function bubbleSort(arr) {
  let n = arr.length;
  for (let i = 0; i < n - 1; i++) {
    for (let j = 0; j < n - i - 1; j++) {
      if (arr[j] > arr[j + 1]) {
        let temp = arr[j];
        arr[j] = arr[j + 1];
        arr[j + 1] = temp;
      }
    }
  }
  return arr;
}
function quickSort(arr) {
  if (arr.length <= 1) return arr;
  let pivot = arr[0];
  let left = [];
  let right = [];
  for (let i = 1; i < arr.length; i++) {
    arr[i] < pivot ? left.push(arr[i]) : right.push(arr[i]);
  }
  return quickSort(left).concat(pivot, quickSort(right));
}

Test Runner

Initializing...

Testing in
Test CaseOps/sec
Native sort
const arr = generateData();
arr.sort((a, b) => a - b);
ready
Bubble Sort
const arr = generateData();
bubbleSort(arr);
ready
Quick Sort
const arr = generateData();
quickSort(arr);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

Revision 1
publishedby Systemon