sort-algorithms (v16)

Revision 16 of this benchmark created by Eugene Scherba on


Description

Comparing native, bubble, insertion, selection, merge, natural merge, and quick sort algorithms on a random array.

Preparation HTML

<script>
/**
 * Define "aij" as the global namespace.
 */
var aij = {
    /**
     * Swap the item in index i with the item in index j.
     * @param {Array.<number>} arr The array containing the items.
     * @param {number} i The index of the first swapped item.
     * @param {number} j The index of the second swapped item.
     */
    swap: function(arr, i, j) {
      var swapped = arr[i];
      arr[i] = arr[j];
      arr[j] = swapped;
    },

    /**
     * Verify that an object is an array with more than one element.
     * @param {*} obj The object to verify.
     * @return {boolean} True if the verified object is an array with at least
     *     one element.
     */
    isSortable: function(obj) {
      return obj && toString.call(obj) === '[object Array]' && obj.length > 1;
    }
};
</script>
<script src="https://raw.github.com/escherba/algorithms-in-javascript/master/src/bubble-sort.js">
</script>
<script src="https://raw.github.com/escherba/algorithms-in-javascript/master/src/insertion-sort.js">
</script>
<script src="https://raw.github.com/escherba/algorithms-in-javascript/master/src/merge-sort.js">
</script>
<script src="https://raw.github.com/escherba/algorithms-in-javascript/master/src/adaptive-sort.js">
</script>
<script src="https://raw.github.com/escherba/algorithms-in-javascript/master/src/quick-sort.js">
</script>
<script src="https://raw.github.com/escherba/algorithms-in-javascript/master/src/selection-sort.js">
</script>

Setup

// Generate array with 10,000 random integers.
    var unsortedTestArray = [];
    for (var i = 0; i < 10000; i++) {
      unsortedTestArray.push(Math.floor(Math.random()*100000));
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Native
unsortedTestArray.sort(function compareNumbers(a, b) {
  return a - b;
});
ready
BubbleSort
aij.bubbleSort(unsortedTestArray);
ready
InsertionSort
aij.insertionSort(unsortedTestArray);
ready
SelectionSort
aij.selectionSort(unsortedTestArray);
ready
MergeSort
aij.mergeSort(unsortedTestArray);
ready
AdaptiveSort
aij.adaptiveSort(unsortedTestArray);
ready
QuickSort
aij.quickSort(unsortedTestArray);
ready

Revisions

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