# Mighty Sort

## JavaScript performance comparison

Test case created by

## Preparation code

``````

<script>
Benchmark.prototype.setup = function() {
"use strict";
const quickSort1 = (() => {
let comp;
const partition = (arr, l = 0, r = arr.length - 1) => {
const pivot = l;
let _l = l + 1;
let _r = r;
while (_l <= _r) {
for (; _l <= r && arr[_l] < arr[pivot]; _l++)
;
for (; _r > l && arr[_r] >= arr[pivot]; _r--)
;
if (comp(_l, _r) < 0) {
[arr[_l], arr[_r]] = [arr[_r], arr[_l]];
}
else {
[arr[pivot], arr[_r]] = [arr[_r], arr[pivot]];
}
}
return _r;
};
const insertionSort = (arr, l = 0, r = arr.length - 1) => {
for (let i = l + 1; i <= r; i++) {
const value = arr[i];
let j = i - 1;
for (; j >= l && comp(value, arr[j]) < 0; j--) {
arr[j + 1] = arr[j];
}
arr[j + 1] = value;
}
return arr;
};
const qsort = (arr, l = 0, r = arr.length - 1) => {
if (r - l <= 50) {
return insertionSort(arr, l, r);
}
const pivot = partition(arr, l, r);
qsort(arr, l, pivot - 1);
qsort(arr, pivot + 1, r);
return arr;
};
return (arr, _comp = (_l, _r) => (_l < _r ? -1 : 1), l = 0, r = arr.length - 1) => {
comp = _comp;
return qsort(arr, l, r);
};
})()

// --------------------------------------------------------------

function range(start, stop, step = 1) {
if (typeof stop == 'undefined') {
stop = start
start = 0
}
if ((step > 0 && start >= stop) || (step < 0 && start <= stop)) {
return []
}
const result = []
for (let i = start; step > 0 ? i < stop : i > stop; i += step) {
result.push(i)
}
return result
}
function randomArray (n, start, stop, step) {
let i, list = []
if (typeof start === 'number') {
start = range(start, stop, step)
}
const {length} = start || list
for (i = 0; i < n; i++) {
if (length) {
list.push(start[(Math.random() * length) | 0])
} else {
list.push(Math.random())
}
}
return list
}

const mightySort = (() => {
const is_safari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent)
return is_safari ? safariSort : sort
function sort (array, compare = (a, b) => a > b, start = 0, end) {
if (end === undefined) {
end = array.length - 1
}
const piv = array[start]
let left = start
let right = end + 1
let rightElement
let leftElement

while (left < --right) if (compare(piv, rightElement = array[right])) while (++left < right) if (compare(leftElement = array[left], piv)) {
array[left] = rightElement
array[right] = leftElement
break
}

if (left !== start) {
array[start] = array[left]
array[left] = piv
}
if (start < left - 1) {
sort(array, compare, start, left - 1)
}
if (left + 1 < end) {
sort(array, compare, left + 1, end)
}
return array
}
function safariSort (array, compare = (a, b) => a > b) {
(function sort (start, end) {
const piv = array[start]
let left = start
let right = end + 1

while (left < --right) if (compare(piv, array[right])) while (++left < right) if (compare(array[left], piv)) {
[array[left], array[right]] = [array[right], array[left]]
break
}

if (left !== start) {
array[start] = array[left]
array[left] = piv
}

if (start < left - 1) {
sort(start, left - 1)
}
if (left + 1 < end) {
sort(left + 1, end)
}
})(0, array.length - 1)
return array
}
})()

function aSort (arr, compare = (a, b) => a > b ? 1 : a < b ? -1 : 0) {
const {length} = arr
let size = 0
let cur
let temp
while (++size < length) {
cur = size - 1
const result = compare(arr[cur], arr[size])
if (result > 0) {
temp = arr[cur]
arr[cur] = arr[size]
arr[size] = temp
while (--cur >= 0) {
const result = compare(arr[cur], arr[cur + 1])
if (result <= 0) break
temp = arr[cur]
arr[cur] = arr[cur + 1]
arr[cur + 1] = temp
}
}
}
}

};

</script>
``````

## Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
mightySort
``mightySort(randomArray(10))``
pending…
quickSort1
``quickSort1(randomArray(10))``
pending…
aSort
``aSort(randomArray(10))``
pending…

## Revisions

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