@kuzanatoliorg/algorithms
Version:
The utils package for react testing library
30 lines (29 loc) • 970 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.cocktailShakerSort = void 0;
const utils_1 = require("../utils");
const cocktailShakerSort = (arr, comparator = utils_1.defaultComparator, order = true) => {
let i, ii, j, item;
for (i = 0, ii = arr.length - 1; i < ii; i++, ii--) {
for (j = 1; j <= ii; j++) {
if (order
? comparator(arr[j], arr[j - 1])
: comparator(arr[j - 1], arr[j])) {
item = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = item;
}
}
for (j = ii; j > i; j--) {
if (order
? comparator(arr[j], arr[j - 1])
: comparator(arr[j - 1], arr[j])) {
item = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = item;
}
}
}
return arr;
};
exports.cocktailShakerSort = cocktailShakerSort;