sort-algorithms (v50)

Revision 50 of this benchmark created on


Description

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

Preparation HTML

<script src="https://rawgithub.com/escherba/algorithms-in-javascript/master/src/common.js">
</script>
<script src="https://rawgithub.com/escherba/algorithms-in-javascript/master/src/bubble-sort.js">
</script>
<script src="https://rawgithub.com/escherba/algorithms-in-javascript/master/src/insertion-sort.js">
</script>
<script src="https://rawgithub.com/escherba/algorithms-in-javascript/master/src/merge-sort.js">
</script>
<script src="https://rawgithub.com/escherba/algorithms-in-javascript/master/src/adaptive-sort.js">
</script>
<script src="https://rawgithub.com/escherba/algorithms-in-javascript/master/src/quickmiddle-sort.js">
</script>
<script src="https://rawgithub.com/escherba/algorithms-in-javascript/master/src/selection-sort.js">
</script>
<script src="https://rawgithub.com/escherba/algorithms-in-javascript/master/src/flashsort.js">
</script>
<script src="https://rawgithub.com/escherba/algorithms-in-javascript/master/src/heap-sort.js">
</script>

Setup

// Generate array with random integers.
    var testArrayLength = 64;
    var unsortedTestArray = new Array(testArrayLength);
    for (var i = 0; i < testArrayLength; i++) {
      unsortedTestArray[i] = Math.floor(Math.random() * testArrayLength);
    }
    var sortedTestArray = unsortedTestArray.clone().sort(function compareNumbers(a, b) {
      return a - b;
    });
    function sortLowToHigh(a, b) {
      return a - b;
    }
    
    function shellSort(a) {
      "use strict";
      for (var h = a.length; h !== 0 ;h = Math.floor(h / 2)) {
        for (var i = h; i < a.length; i++) {
          var k = a[i];
          for (var j = i; j >= h && k < a[j - h]; j -= h) {
            a[j] = a[j - h];
          }
          a[j] = k;
        }
        
      }
      return a;
    }

Teardown


    if (!result.compare(sortedTestArray)) {
       throw new Error("Array was not sorted");
    }
  

Test runner

Ready to run.

Testing in
TestOps/sec
Native
var result = unsortedTestArray.clone().sort(function(a, b) {
  return a - b;
});
ready
InsertionSort
var result = aij.insertionSort(unsortedTestArray.clone());
ready
Heapsort
var result = aij.heapSort(unsortedTestArray.clone());
ready
AdaptiveSort
var result = aij.adaptiveSort(unsortedTestArray.clone());
ready
MergeSort
var result = aij.mergeSort(unsortedTestArray.clone());
ready
QuickSort
var result = aij.quickmiddleSort(unsortedTestArray.clone());
ready
Flashsort
var result = aij.flashSort(unsortedTestArray.clone());
ready
Shellsort
var result = shellSort(unsortedTestArray.clone());
ready

Revisions

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