UNPKG

area51

Version:

Experimental prototypes of alien things built in JavaScript. The bits may end up living in a different package.

39 lines (31 loc) 917 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = insertionSort; var _arrayUtil = require("./array-util"); function performSort(array, length) { var n = length; for (var i = 1; i < n; i++) { for (var j = i; j > 0 && array[j] < array[j - 1]; j--) { (0, _arrayUtil.swap)(array, j, j - 1); } } } function performSortWithCompare(array, length, compare) { var n = length; for (var i = 1; i < n; i++) { for (var j = i; j > 0 && compare(array[j], array[j - 1]) === -1; j--) { (0, _arrayUtil.swap)(array, j, j - 1); } } } function insertionSort(array, length, compare) { if (arguments.length === 2 && typeof length === "function") { compare = length; length = array.length; } length = length || array.length; if (compare) performSortWithCompare(array, length, compare);else performSort(array, length); } module.exports = exports["default"];