Skip to content

Commit

Permalink
feat: version 0.0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
santi100a committed Aug 22, 2023
1 parent 148035d commit 33c373a
Show file tree
Hide file tree
Showing 24 changed files with 688 additions and 613 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@

- Fixed ESM wrapping issue.
- Implemented code splitting.

## Version 0.0.5

- Added `bozoSort`.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,25 @@ largest element.
**Time complexity (best, average and worse):** $O(n + k)$, where $k$ is the range of input (maximum
element - minimum element + 1).

- `function bozoSort<T = unknown>(array: T[]): T[];` Sorts an array using the Bozo Sort algorithm.

| Name | Description |
| ------- | ----------------------- |
| `array` | The array to be sorted. |

- `function bozoSort<T = unknown>(array: T[], opts: SortingOptions<T>): T[];`
Sorts an array using the Bozo Sort algorithm.

| Name | Description |
| ------- | ------------------------------------- |
| `array` | The array to be sorted. |
| `opts` | An object containing sorting options. |

## Usage

```typescript
import { mergeSort } from '@santi100/sorting-lib'; // ESM
const { mergeSort } = require('@santi100/sorting-lib'); // CJS
import { mergeSort, bozoSort } from '@santi100/sorting-lib'; // ESM
const { mergeSort, bozoSort } = require('@santi100/sorting-lib'); // CJS
const sorted = mergeSort([4, 2, 5, 1, 3]); // sorted = [1, 2, 3, 4, 5]
const descendingSorted = mergeSort([4, 2, 5, 1, 3], { order: 'descending' }); // descendingSorted = [5, 4, 3, 2, 1]
const objSorted = mergeSort(
Expand All @@ -141,6 +155,8 @@ const objSorted = mergeSort(
); // returns [ { age: 12 }, { age: 23 }, { age: 30 }]
// You can do same for all algorithms, except for `radixSort`, which is limited to ints for now, so
// its only option is `order`.
const sortedArray = bozoSort(array);
console.log(sortedArray); // Output: [1, 2, 3]
```

## Contribute
Expand Down
74 changes: 26 additions & 48 deletions cjs/bogo-sort.js
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;
};
17 changes: 17 additions & 0 deletions cjs/bozo-sort.d.ts
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;
37 changes: 37 additions & 0 deletions cjs/bozo-sort.js
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;
56 changes: 23 additions & 33 deletions cjs/bubble-sort.js
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;
};
33 changes: 17 additions & 16 deletions cjs/core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,34 @@ export type SortOrder = 'ascending' | 'descending';
* See {@link RadixSortOptions} for the options specific to it.
*/
export interface SortOptions<T = unknown> {
/**
* Comparator function for every sorting algorithm, except for {@link radixSort}.
* It's fully compatible with {@link Array.prototype.sort}'s callback. See {@link SortComparator}.
*
* **Keep in mind this option overrides the `order` option.**
*/
comparator?: SortComparator<T>;
/**
* Sorting order string. Must be either `ascending` or `descending`. See {@link SortOrder}.
*/
order?: SortOrder;
/**
* Comparator function for every sorting algorithm, except for {@link radixSort}.
* It's fully compatible with {@link Array.prototype.sort}'s callback. See {@link SortComparator}.
*
* **Keep in mind this option overrides the `order` option.**
*/
comparator?: SortComparator<T>;
/**
* Sorting order string. Must be either `ascending` or `descending`. See {@link SortOrder}.
*/
order?: SortOrder;
}
/**
* Shape of the `opts` object exclusive to {@link radixSort}.
*/
export interface RadixSortOptions {
/**
* Sorting order string. Must be either `ascending` or `descending`. See {@link SortOrder}.
*/
order?: SortOrder;
/**
* Sorting order string. Must be either `ascending` or `descending`. See {@link SortOrder}.
*/
order?: SortOrder;
}
/**
* Shape of the `opts` object exclusive to {@link countingSort}.
* Shape of the `opts` object exclusive to `countingSort`.
* @since 0.0.3
*/
export type CountingSortOptions = RadixSortOptions;
export declare function __defAscending(a: unknown, b: unknown): 1 | -1 | 0;
export declare function __reverse<T>(arr: T[]): T[];
export declare function __defDescending(a: unknown, b: unknown): 1 | -1 | 0;
export declare function __isInteger(num: number): boolean;
export declare function __isSorted<T>(arr: T[], comparator: SortComparator<T>): boolean;
65 changes: 40 additions & 25 deletions cjs/core.js
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;
Loading

0 comments on commit 33c373a

Please sign in to comment.