-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
688 additions
and
613 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,7 @@ | |
|
||
- Fixed ESM wrapping issue. | ||
- Implemented code splitting. | ||
|
||
## Version 0.0.5 | ||
|
||
- Added `bozoSort`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,28 @@ | ||
'use strict'; | ||
var __spreadArray = | ||
(this && this.__spreadArray) || | ||
function (to, from, pack) { | ||
if (pack || arguments.length === 2) | ||
for (var i = 0, l = from.length, ar; i < l; i++) { | ||
if (ar || !(i in from)) { | ||
if (!ar) ar = Array.prototype.slice.call(from, 0, i); | ||
ar[i] = from[i]; | ||
} | ||
} | ||
return to.concat(ar || Array.prototype.slice.call(from)); | ||
}; | ||
var assertDefined = require('@santi100/assertion-lib/cjs/defined'); | ||
var assertInstanceOf = require('@santi100/assertion-lib/cjs/instance-of'); | ||
var assertOneOf = require('@santi100/assertion-lib/cjs/one-of'); | ||
var assertTypeOf = require('@santi100/assertion-lib/cjs/type-of'); | ||
var shuffle = require('@santi100/array-shuffle'); | ||
var core_1 = require('./core'); | ||
"use strict"; | ||
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { | ||
if (ar || !(i in from)) { | ||
if (!ar) ar = Array.prototype.slice.call(from, 0, i); | ||
ar[i] = from[i]; | ||
} | ||
} | ||
return to.concat(ar || Array.prototype.slice.call(from)); | ||
}; | ||
var assertDefined = require("@santi100/assertion-lib/cjs/defined"); | ||
var assertInstanceOf = require("@santi100/assertion-lib/cjs/instance-of"); | ||
var assertOneOf = require("@santi100/assertion-lib/cjs/one-of"); | ||
var assertTypeOf = require("@santi100/assertion-lib/cjs/type-of"); | ||
var shuffle = require("@santi100/array-shuffle"); | ||
var core_1 = require("./core"); | ||
module.exports = function bogoSort(arr, opts) { | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
assertInstanceOf(arr, Array, 'arr'); | ||
assertDefined(opts, 'opts'); | ||
assertTypeOf(opts, 'object'); | ||
var _a = opts.order, | ||
order = _a === void 0 ? 'ascending' : _a, | ||
_b = opts.comparator, | ||
comparator = | ||
_b === void 0 | ||
? order === 'ascending' | ||
? core_1.__defAscending | ||
: core_1.__defDescending | ||
: _b; | ||
assertOneOf(order, 'opts.order', ['ascending', 'descending']); | ||
function __isSorted(arr) { | ||
var array = __spreadArray([], arr, true); | ||
var sorted = true; | ||
for (var i = 0; i < array.length - 1; i++) { | ||
if (comparator(array[i + 1], array[i]) < 0) { | ||
sorted = false; | ||
} | ||
} | ||
return sorted; | ||
} | ||
var array = __spreadArray([], arr, true); | ||
while (!__isSorted(array)) array = shuffle(array); | ||
return array; | ||
if (opts === void 0) { opts = {}; } | ||
assertInstanceOf(arr, Array, 'arr'); | ||
assertDefined(opts, 'opts'); | ||
assertTypeOf(opts, 'object'); | ||
var _a = opts.order, order = _a === void 0 ? 'ascending' : _a, _b = opts.comparator, comparator = _b === void 0 ? order === 'ascending' ? core_1.__defAscending : core_1.__defDescending : _b; | ||
assertOneOf(order, 'opts.order', ['ascending', 'descending']); | ||
var array = __spreadArray([], arr, true); | ||
while (!(0, core_1.__isSorted)(array, comparator)) | ||
array = shuffle(array); | ||
return array; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { SortOptions } from './core'; | ||
/** | ||
* Sorts an array using the Bozo Sort algorithm. | ||
* | ||
* @template T The type of elements in the array. | ||
* @param {T[]} array The array to be sorted. | ||
*/ | ||
declare function bozoSort<T = unknown>(array: T[]): T[]; | ||
/** | ||
* Sorts an array using the Bozo Sort algorithm. | ||
* | ||
* @template T The type of elements in the array. | ||
* @param {T[]} array The array to be sorted. | ||
* @param {SortOptions} [opts] An object containing sorting options. | ||
*/ | ||
declare function bozoSort<T = unknown>(array: T[], opts: SortOptions<T>): T[]; | ||
export = bozoSort; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
"use strict"; | ||
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { | ||
if (ar || !(i in from)) { | ||
if (!ar) ar = Array.prototype.slice.call(from, 0, i); | ||
ar[i] = from[i]; | ||
} | ||
} | ||
return to.concat(ar || Array.prototype.slice.call(from)); | ||
}; | ||
var core_1 = require("./core"); | ||
var randomIntegers = require("@santi100/random-lib/cjs/random-integers"); | ||
var assertArray = require("@santi100/assertion-lib/cjs/array"); | ||
var assertDefined = require("@santi100/assertion-lib/cjs/defined"); | ||
var assertTypeOf = require("@santi100/assertion-lib/cjs/type-of"); | ||
var assertOneOf = require("@santi100/assertion-lib/cjs/one-of"); | ||
function bozoSort(array, opts) { | ||
var _a; | ||
if (opts === void 0) { opts = { | ||
order: 'ascending', | ||
comparator: core_1.__defAscending | ||
}; } | ||
var arr = __spreadArray([], array, true); | ||
assertArray(array, 'array'); | ||
assertDefined(opts, 'opts'); | ||
assertTypeOf(opts, 'object', 'opts'); | ||
var _b = opts.order, order = _b === void 0 ? 'ascending' : _b, _c = opts.comparator, comparator = _c === void 0 ? order === 'ascending' ? core_1.__defAscending : core_1.__defDescending : _c; | ||
assertTypeOf(order, 'string', 'order'); | ||
assertOneOf(order, 'order', ['ascending', 'descending']); | ||
assertTypeOf(comparator, 'function', 'comparator'); | ||
while (!(0, core_1.__isSorted)(arr, comparator)) { | ||
var _d = randomIntegers(2, { max: array.length, min: 0 }), i = _d[0], j = _d[1]; | ||
_a = [arr[j], arr[i]], arr[i] = _a[0], arr[j] = _a[1]; // swap | ||
} | ||
return arr; | ||
} | ||
module.exports = bozoSort; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,25 @@ | ||
'use strict'; | ||
var assertDefined = require('@santi100/assertion-lib/cjs/defined'); | ||
var assertInstanceOf = require('@santi100/assertion-lib/cjs/instance-of'); | ||
var assertOneOf = require('@santi100/assertion-lib/cjs/one-of'); | ||
var assertTypeOf = require('@santi100/assertion-lib/cjs/type-of'); | ||
var core_1 = require('./core'); | ||
"use strict"; | ||
var assertDefined = require("@santi100/assertion-lib/cjs/defined"); | ||
var assertInstanceOf = require("@santi100/assertion-lib/cjs/instance-of"); | ||
var assertOneOf = require("@santi100/assertion-lib/cjs/one-of"); | ||
var assertTypeOf = require("@santi100/assertion-lib/cjs/type-of"); | ||
var core_1 = require("./core"); | ||
module.exports = function bubbleSort(arr, opts) { | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
assertInstanceOf(arr, Array, 'arr'); | ||
assertDefined(opts, 'opts'); | ||
assertTypeOf(opts, 'object'); | ||
var array = arr.slice(); | ||
var _a = opts.order, | ||
order = _a === void 0 ? 'ascending' : _a, | ||
_b = opts.comparator, | ||
comparator = | ||
_b === void 0 | ||
? order === 'ascending' | ||
? core_1.__defAscending | ||
: core_1.__defDescending | ||
: _b; | ||
assertOneOf(order, 'opts.order', ['ascending', 'descending']); | ||
for (var i = 0; i < array.length; i++) { | ||
for (var j = 0; j < array.length - i - 1; j++) { | ||
if (comparator(array[j], array[j + 1]) > 0) { | ||
var temp = array[j]; | ||
array[j] = array[j + 1]; | ||
array[j + 1] = temp; | ||
} | ||
} | ||
} | ||
return array; | ||
if (opts === void 0) { opts = {}; } | ||
assertInstanceOf(arr, Array, 'arr'); | ||
assertDefined(opts, 'opts'); | ||
assertTypeOf(opts, 'object'); | ||
var array = arr.slice(); | ||
var _a = opts.order, order = _a === void 0 ? 'ascending' : _a, _b = opts.comparator, comparator = _b === void 0 ? order === 'ascending' ? core_1.__defAscending : core_1.__defDescending : _b; | ||
assertOneOf(order, 'opts.order', ['ascending', 'descending']); | ||
for (var i = 0; i < array.length; i++) { | ||
for (var j = 0; j < array.length - i - 1; j++) { | ||
if (comparator(array[j], array[j + 1]) > 0) { | ||
var temp = array[j]; | ||
array[j] = array[j + 1]; | ||
array[j + 1] = temp; | ||
} | ||
} | ||
} | ||
return array; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,53 @@ | ||
'use strict'; | ||
"use strict"; | ||
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { | ||
if (ar || !(i in from)) { | ||
if (!ar) ar = Array.prototype.slice.call(from, 0, i); | ||
ar[i] = from[i]; | ||
} | ||
} | ||
return to.concat(ar || Array.prototype.slice.call(from)); | ||
}; | ||
exports.__esModule = true; | ||
exports.__isInteger = | ||
exports.__defDescending = | ||
exports.__reverse = | ||
exports.__defAscending = | ||
void 0; | ||
exports.__isSorted = exports.__isInteger = exports.__defDescending = exports.__reverse = exports.__defAscending = void 0; | ||
function __defAscending(a, b) { | ||
// @ts-expect-error It's fine to have "unknown". | ||
if (a < b) return -1; | ||
// @ts-expect-error It's fine to have "unknown". | ||
if (a > b) return 1; | ||
return 0; | ||
// @ts-expect-error It's fine to have "unknown". | ||
if (a < b) | ||
return -1; | ||
// @ts-expect-error It's fine to have "unknown". | ||
if (a > b) | ||
return 1; | ||
return 0; | ||
} | ||
exports.__defAscending = __defAscending; | ||
function __reverse(arr) { | ||
return arr.slice().reverse(); | ||
return arr.slice().reverse(); | ||
} | ||
exports.__reverse = __reverse; | ||
function __defDescending(a, b) { | ||
// @ts-expect-error It's fine to have "unknown". | ||
if (a < b) return 1; | ||
// @ts-expect-error It's fine to have "unknown". | ||
if (a > b) return -1; | ||
return 0; | ||
// @ts-expect-error It's fine to have "unknown". | ||
if (a < b) | ||
return 1; | ||
// @ts-expect-error It's fine to have "unknown". | ||
if (a > b) | ||
return -1; | ||
return 0; | ||
} | ||
exports.__defDescending = __defDescending; | ||
function __isInteger(num) { | ||
var _a; | ||
return ( | ||
((_a = Number === null || Number === void 0 ? void 0 : Number.isInteger) === | ||
null || _a === void 0 | ||
? void 0 | ||
: _a.call(Number, num)) || | ||
(num < 0 ? Math.ceil(num) : Math.floor(num)) === num | ||
); | ||
var _a; | ||
return (((_a = Number === null || Number === void 0 ? void 0 : Number.isInteger) === null || _a === void 0 ? void 0 : _a.call(Number, num)) || | ||
(num < 0 ? Math.ceil(num) : Math.floor(num)) === num); | ||
} | ||
exports.__isInteger = __isInteger; | ||
function __isSorted(arr, comparator) { | ||
var array = __spreadArray([], arr, true); | ||
var sorted = true; | ||
for (var i = 0; i < array.length - 1; i++) { | ||
if (comparator(array[i + 1], array[i]) < 0) { | ||
sorted = false; | ||
} | ||
} | ||
return sorted; | ||
} | ||
exports.__isSorted = __isSorted; |
Oops, something went wrong.