From a4f225034537c529add94987d284267945ca4759 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B8=B8=E6=B0=B4=E8=8A=A6=E8=8B=87?= Date: Fri, 12 Jan 2024 08:31:26 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9A=20docs(docs):=20Explain=20that=20t?= =?UTF-8?q?he=20document=20has=20been=20perfected?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When modifying the instruction document, the code is partially improved by the way --- .github/workflows/ci.yml | 4 +- README.md | 132 ++++++++++++++++++++++++++++++++++++ README.zh-CN.md | 110 ++++++++++++++++++++++++------ configs/.ks-cvlarrc.cjs | 2 +- package.json | 28 ++++---- src/compare.cjs | 61 +++++++---------- src/index.ts | 17 ++--- src/sorts/cocktail/index.ts | 2 +- src/sorts/pancake/index.ts | 2 +- vite.config.ts | 2 +- 10 files changed, 273 insertions(+), 87 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 704804c..0890a08 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -50,8 +50,8 @@ jobs: env: SSH_PRIVATE_KEY: ${{ secrets.GITEE_PRIVATE_KEY }} with: - source-repo: 'git@github.com:kwooshung/algorithms-sorts.git' - destination-repo: 'git@gitee.com:kwooshung/algorithms-sorts.git' + source-repo: 'git@github.com:kwooshung/algorithm-sorts.git' + destination-repo: 'git@gitee.com:kwooshung/algorithm-sorts.git' # 自动发布到 npm - name: Sync to Npm.js diff --git a/README.md b/README.md index e69de29..b7156ea 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,132 @@ +
+ +# @kwooshung/algorithm-sorts + +![GitHub Release Date - Published_At](https://img.shields.io/github/release-date/kwooshung/algorithm-sorts?labelColor=272e3b&color=00b42A&logo=github) +![GitHub last commit](https://img.shields.io/github/last-commit/kwooshung/algorithm-sorts?labelColor=272e3b&color=165dff) +![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/kwooshung/algorithm-sorts?labelColor=272e3b&color=165dff) +![GitHub top language](https://img.shields.io/github/languages/top/kwooshung/algorithm-sorts?labelColor=272e3b&color=165dff) +![GitHub pull requests](https://img.shields.io/github/issues-pr/kwooshung/algorithm-sorts?labelColor=272e3b&color=165dff) +![GitHub issues](https://img.shields.io/github/issues/kwooshung/algorithm-sorts?labelColor=272e3b&color=165dff) +[![NPM Version](https://img.shields.io/npm/v/@kwooshung/algorithm-sorts?labelColor=272e3b&color=165dff)](https://www.npmjs.com/package/@kwooshung/algorithm-sorts) +[![Npm.js Downloads/Week](https://img.shields.io/npm/dw/@kwooshung/algorithm-sorts?labelColor=272e3b&labelColor=272e3b&color=165dff&logo=npm)](https://www.npmjs.com/package/@kwooshung/algorithm-sorts) +[![Github CI/CD](https://github.com/kwooshung/algorithm-sorts/actions/workflows/ci.yml/badge.svg)](https://github.com/kwooshung/algorithm-sorts/actions/) +[![codecov](https://codecov.io/gh/kwooshung/algorithm-sorts/graph/badge.svg?token=VVZJE7H0KD)](https://codecov.io/gh/kwooshung/algorithm-sorts) +[![Maintainability](https://api.codeclimate.com/v1/badges/325d0881b1ca19165d35/maintainability)](https://codeclimate.com/github/kwooshung/algorithm-sorts/maintainability/) +[![GitHub License](https://img.shields.io/github/license/kwooshung/algorithm-sorts?labelColor=272e3b&color=165dff)](LICENSE) +[![Gitee Repo](https://img.shields.io/badge/gitee-cvlar-165dff?logo=gitee)](https://gitee.com/kwooshung/algorithm-sorts/) +![Github Stars](https://img.shields.io/github/stars/kwooshung/algorithm-sorts?labelColor=272e3b&color=165dff) + +

+ 中文 | + English +

+
+ +# Why was it developed? + +- During development, I needed to use algorithms and found the [algorithms.js](https://github.com/felipernb/algorithms.js) library. Its implementation is excellent. This project is based on [algorithms.js](https://github.com/felipernb/algorithms.js), with slight modifications in the [compare](src/internal/compare/index.ts) section. +- [algorithms.js](https://github.com/felipernb/algorithms.js) is implemented using the `commonjs` standard, not `esm`. This means it can't utilize `tree shaking`. While there are indirect methods to achieve this, they are not as convenient as using the `esm` standard directly. +- [algorithms.js](https://github.com/felipernb/algorithms.js) hasn't been maintained for several years. Some algorithms and implementation logic would be better with a modern approach. + +# Why Use It? + +- Written in `Typescript` with unit testing, ensuring code quality with `100%` coverage. +- Supports both `esm` and `commonjs` standards. The `esm` standard enables direct use of `tree shaking` to reduce the bundle size. +- Clear documentation, including comments in both Chinese and English, making it easy to read and understand, as shown below. + +```ts +/** + * 短冒泡排序 / Short Bubble Sort + * @description 短冒泡排序是冒泡排序的一种变体,当在整个排序过程中没有进行任何交换时,该算法会提前停止 / Short bubble sort is a variation of bubble sort that stops early if no swaps are made during the entire sorting process + * @usageScenario 适用于检测几乎已经排序好的数组 / Suitable for detecting nearly sorted arrays + * @timeComplexity 最好情况 O(n),平均和最坏情况 O(n^2) / Best case O(n), average and worst case O(n^2) + * @param {T[]} array 待排序数组 / Array to be sorted + * @param {boolean} [modifyOriginal=true] 是否修改原数组 / Whether to modify the original array + * @param {(a: T, b: T) => number} [compareFunction] 比较函数,定义元素的排序方式 / Comparison function, defines the sorting order of elements + * @param {boolean} [reverse=false] 是否反转结果 / Whether to reverse the result + */ +``` + +- Compared to [algorithms.js](https://github.com/felipernb/algorithms.js), our sorting algorithms include several additional ones, with more to be added in the future. +- In most cases, the efficiency of our library's algorithms is superior to [algorithms.js](https://github.com/felipernb/algorithms.js). There are comparative charts below, and you can also `git clone` this project and run `npm compare` for testing. + +# When Not to Use It? + +As the project name suggests, our project's algorithms only support `sorting`. If you need other types of algorithms, you might want to use [algorithms.js](https://github.com/felipernb/algorithms.js). + +# Efficiency Comparison + +The comparison involves random data of 1000 items, with string lengths varying from 6 to 32 and numbers ranging from 0 to 1,000,000, as shown below: + +| Algorithm | algorithms.js | @kwooshung/algorithm-sorts | Difference | +| :---------------------- | :-----------: | :------------------------: | :------------: | +| Bubble Sort | 65.0368 ms | 65.0298 ms | -0.0070 ms | +| Shout Bubble Sort | 13251.3260 ms | 128.2500 ms | -13123.0760 ms | +| Cocktail Sort | - | 52.7166 ms | - | +| Counting Sort | - | 12.3503 ms | - | +| Optimized Counting Sort | 33.2357 ms | 32.6380 ms | -0.5977 ms | +| Heap Sort | 8.3025 ms | 4.6525 ms | -3.6500 ms | +| Insertion Sort | 27.4480 ms | 27.4331 ms | -0.0149 ms | +| Merge Sort | 2.9167 ms | 2.5592 ms | -0.3575 ms | +| Pancake Sort | - | 57.7009 ms | 0 ms | +| Quick Sort | 3.0599 ms | 2.6374 ms | -0.4225 ms | +| Radix Sort | 0.2070 ms | 0.1339 ms | -0.0731 ms | +| Selection Sort | 55.8389 ms | 55.8000 ms | -0.0389 ms | +| Shell Sort | 3.1775 ms | 3.1564 ms | -0.0211 ms | +| Tim Sort | - | 6.7950 ms | - | + +# Install + +## npm + +```bash +npm install @kwooshung/algorithm-sorts --save-dev +``` + +## yarn + +```bash +yarn add @kwooshung/algorithm-sorts -D +``` + +## pnpm + +```bash +pnpm add @kwooshung/algorithm-sorts -D +``` + +# Usage + +## Importing + +### esm + +```ts +import { bubbleSort } from '@kwooshung/algorithm-sorts'; +``` + +### commonjs + +```ts +const { bubbleSort } = require('@kwooshung/algorithm-sorts'); +``` + +## Supported Algorithms + +> Currently, the following sorting algorithms are supported. For specific usage instructions, click on the sorting algorithm names below and refer to the comments and parameters: + +- [» Bubble Sort](src/sorts/bubble/index.ts) +- [» Short Bubble Sort](src/sorts/bubble/short/index.ts) +- [» Cocktail Sort](src/sorts/cocktail/index.ts) +- [» Counting Sort](src/sorts/counting/index.ts) +- [» Counting Sort (Optimized Version)](src/sorts/counting/optimized/index.ts) +- [» Heap Sort](src/sorts/heap/index.ts) +- [» Insertion Sort](src/sorts/insertion/index.ts) +- [» Merge Sort](src/sorts/merge/index.ts) +- [» Pancake Sorting](src/sorts/pancake/index.ts) +- [» Quick Sort](src/sorts/quick/index.ts) +- [» Radix Sort](src/sorts/radix/index.ts) +- [» Selection Sort](src/sorts/selection/index.ts) +- [» Shell Sort](src/sorts/shell/index.ts) +- [» Tim Sort](src/sorts/tim/index.ts) diff --git a/README.zh-CN.md b/README.zh-CN.md index a845724..2882495 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -1,21 +1,21 @@
-# @kwooshung/algorithms-sorts - -![GitHub Release Date - Published_At](https://img.shields.io/github/release-date/kwooshung/algorithms-sorts?labelColor=272e3b&color=00b42A&logo=github) -![GitHub last commit](https://img.shields.io/github/last-commit/kwooshung/algorithms-sorts?labelColor=272e3b&color=165dff) -![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/kwooshung/algorithms-sorts?labelColor=272e3b&color=165dff) -![GitHub top language](https://img.shields.io/github/languages/top/kwooshung/algorithms-sorts?labelColor=272e3b&color=165dff) -![GitHub pull requests](https://img.shields.io/github/issues-pr/kwooshung/algorithms-sorts?labelColor=272e3b&color=165dff) -![GitHub issues](https://img.shields.io/github/issues/kwooshung/algorithms-sorts?labelColor=272e3b&color=165dff) -[![NPM Version](https://img.shields.io/npm/v/@kwooshung/algorithms-sorts?labelColor=272e3b&color=165dff)](https://www.npmjs.com/package/@kwooshung/algorithms-sorts) -[![Npm.js Downloads/Week](https://img.shields.io/npm/dw/@kwooshung/algorithms-sorts?labelColor=272e3b&labelColor=272e3b&color=165dff&logo=npm)](https://www.npmjs.com/package/@kwooshung/algorithms-sorts) -[![Github CI/CD](https://github.com/kwooshung/algorithms-sorts/actions/workflows/ci.yml/badge.svg)](https://github.com/kwooshung/algorithms-sorts/actions/) -[![codecov](https://codecov.io/gh/kwooshung/algorithms-sorts/graph/badge.svg?token=VVZJE7H0KD)](https://codecov.io/gh/kwooshung/algorithms-sorts) -[![Maintainability](https://api.codeclimate.com/v1/badges/325d0881b1ca19165d35/maintainability)](https://codeclimate.com/github/kwooshung/algorithms-sorts/maintainability/) -[![GitHub License](https://img.shields.io/github/license/kwooshung/algorithms-sorts?labelColor=272e3b&color=165dff)](LICENSE) -[![Gitee Repo](https://img.shields.io/badge/gitee-cvlar-165dff?logo=gitee)](https://gitee.com/kwooshung/algorithms-sorts/) -![Github Stars](https://img.shields.io/github/stars/kwooshung/algorithms-sorts?labelColor=272e3b&color=165dff) +# @kwooshung/algorithm-sorts + +![GitHub Release Date - Published_At](https://img.shields.io/github/release-date/kwooshung/algorithm-sorts?labelColor=272e3b&color=00b42A&logo=github) +![GitHub last commit](https://img.shields.io/github/last-commit/kwooshung/algorithm-sorts?labelColor=272e3b&color=165dff) +![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/kwooshung/algorithm-sorts?labelColor=272e3b&color=165dff) +![GitHub top language](https://img.shields.io/github/languages/top/kwooshung/algorithm-sorts?labelColor=272e3b&color=165dff) +![GitHub pull requests](https://img.shields.io/github/issues-pr/kwooshung/algorithm-sorts?labelColor=272e3b&color=165dff) +![GitHub issues](https://img.shields.io/github/issues/kwooshung/algorithm-sorts?labelColor=272e3b&color=165dff) +[![NPM Version](https://img.shields.io/npm/v/@kwooshung/algorithm-sorts?labelColor=272e3b&color=165dff)](https://www.npmjs.com/package/@kwooshung/algorithm-sorts) +[![Npm.js Downloads/Week](https://img.shields.io/npm/dw/@kwooshung/algorithm-sorts?labelColor=272e3b&labelColor=272e3b&color=165dff&logo=npm)](https://www.npmjs.com/package/@kwooshung/algorithm-sorts) +[![Github CI/CD](https://github.com/kwooshung/algorithm-sorts/actions/workflows/ci.yml/badge.svg)](https://github.com/kwooshung/algorithm-sorts/actions/) +[![codecov](https://codecov.io/gh/kwooshung/algorithm-sorts/graph/badge.svg?token=VVZJE7H0KD)](https://codecov.io/gh/kwooshung/algorithm-sorts) +[![Maintainability](https://api.codeclimate.com/v1/badges/325d0881b1ca19165d35/maintainability)](https://codeclimate.com/github/kwooshung/algorithm-sorts/maintainability/) +[![GitHub License](https://img.shields.io/github/license/kwooshung/algorithm-sorts?labelColor=272e3b&color=165dff)](LICENSE) +[![Gitee Repo](https://img.shields.io/badge/gitee-cvlar-165dff?logo=gitee)](https://gitee.com/kwooshung/algorithm-sorts/) +![Github Stars](https://img.shields.io/github/stars/kwooshung/algorithm-sorts?labelColor=272e3b&color=165dff)

English | @@ -33,7 +33,21 @@ - `Typescript` 编写,并进行了单元测试,保证了代码质量,代码覆盖率达到 `100%`; - 支持 `esm` 和 `commonjs` 规范,`esm` 规范可以直接使用 `树摇(tree-shaking)`,减少打包体积; -- 清晰的注释,包含中英文注释,方便阅读和理解; +- 清晰的注释,包含中英文注释,方便阅读和理解,如下所示; + +```ts +/** + * 短冒泡排序 / Short Bubble Sort + * @description 短冒泡排序是冒泡排序的一种变体,当在整个排序过程中没有进行任何交换时,该算法会提前停止 / Short bubble sort is a variation of bubble sort that stops early if no swaps are made during the entire sorting process + * @usageScenario 适用于检测几乎已经排序好的数组 / Suitable for detecting nearly sorted arrays + * @timeComplexity 最好情况 O(n),平均和最坏情况 O(n^2) / Best case O(n), average and worst case O(n^2) + * @param {T[]} array 待排序数组 / Array to be sorted + * @param {boolean} [modifyOriginal=true] 是否修改原数组 / Whether to modify the original array + * @param {(a: T, b: T) => number} [compareFunction] 比较函数,定义元素的排序方式 / Comparison function, defines the sorting order of elements + * @param {boolean} [reverse=false] 是否反转结果 / Whether to reverse the result + */ +``` + - 排序算法相比 [algorithms.js](https://github.com/felipernb/algorithms.js) 多了几个,后续会继续添加; - 大部分情况下,本库算法效率优于 [algorithms.js](https://github.com/felipernb/algorithms.js),下方有对比图表,您也可以 git clone 本项目,运行 `npm compare` 进行测试; @@ -41,24 +55,78 @@ 就如项目名称而言,你也应该知道,本项目的算法只支持 `排序`,如果你需要其他算法,可以使用 [algorithms.js](https://github.com/felipernb/algorithms.js) +# 效率对比 + +随机 1000 条数据,字符串长度在 6 到 32 之间,数字在 0 到 1,000,000 之间,对比如下: + +| 算法 | algorithms.js | @kwooshung/algorithm-sorts | 差异 | +| :------------------- | :-----------: | :------------------------: | :------------: | +| 冒泡排序 | 65.0368 ms | 65.0298 ms | -0.0070 ms | +| 短冒泡排序 | 13251.3260 ms | 128.2500 ms | -13123.0760 ms | +| 鸡尾酒排序 | - | 52.7166 ms | - | +| 计数排序 | - | 12.3503 ms | - | +| 计数排序(优化版本) | 33.2357 ms | 32.6380 ms | -0.5977 ms | +| 堆排序 | 8.3025 ms | 4.6525 ms | -3.6500 ms | +| 插入排序 | 27.4480 ms | 27.4331 ms | -0.0149 ms | +| 归并排序 | 2.9167 ms | 2.5592 ms | -0.3575 ms | +| 煎饼排序 | - | 57.7009 ms | 0 ms | +| 快速排序 | 3.0599 ms | 2.6374 ms | -0.4225 ms | +| 基数排序 | 0.2070 ms | 0.1339 ms | -0.0731 ms | +| 选择排序 | 55.8389 ms | 55.8000 ms | -0.0389 ms | +| 希尔排序 | 3.1775 ms | 3.1564 ms | -0.0211 ms | +| 定向排序 | - | 6.7950 ms | - | + # 安装 ## npm ```bash -npm install @kwooshung/algorithms-sorts --save-dev +npm install @kwooshung/algorithm-sorts --save-dev ``` ## yarn ```bash -yarn add @kwooshung/algorithms-sorts -D +yarn add @kwooshung/algorithm-sorts -D ``` ## pnpm ```bash -pnpm add @kwooshung/algorithms-sorts -D +pnpm add @kwooshung/algorithm-sorts -D +``` + +# 使用方法 + +## 引入 + +### esm + +```ts +import { bubbleSort } from '@kwooshung/algorithm-sorts'; +``` + +### commonjs + +```ts +const { bubbleSort } = require('@kwooshung/algorithm-sorts'); ``` -# 使用 +## 已支持的算法 + +> 目前已支持如下排序算法,具体使用方法,点击下方排序名称,参考注释和参数即可: + +- [» 冒泡排序](src/sorts/bubble/index.ts) +- [» 短冒泡排序](src/sorts/bubble/short/index.ts) +- [» 鸡尾酒排序](src/sorts/cocktail/index.ts) +- [» 计数排序](src/sorts/counting/index.ts) +- [» 计数排序(优化版本)](src/sorts/counting/optimized/index.ts) +- [» 堆排序](src/sorts/heap/index.ts) +- [» 插入排序](src/sorts/insertion/index.ts) +- [» 归并排序](src/sorts/merge/index.ts) +- [» 煎饼排序 ](src/sorts/pancake/index.ts) +- [» 快速排序](src/sorts/quick/index.ts) +- [» 基数排序](src/sorts/radix/index.ts) +- [» 选择排序](src/sorts/selection/index.ts) +- [» 希尔排序](src/sorts/shell/index.ts) +- [» 定序排序](src/sorts/tim/index.ts) diff --git a/configs/.ks-cvlarrc.cjs b/configs/.ks-cvlarrc.cjs index 63f1ab7..1f3195f 100644 --- a/configs/.ks-cvlarrc.cjs +++ b/configs/.ks-cvlarrc.cjs @@ -185,7 +185,7 @@ module.exports = { * 可使用的变量: * id:表示提交id,完整的ID一般为40位 */ - url: 'https://github.com/kwooshung/algorithms-sorts/commit/{{id}}' + url: 'https://github.com/kwooshung/algorithm-sorts/commit/{{id}}' } } } diff --git a/package.json b/package.json index 3232024..7af70d2 100644 --- a/package.json +++ b/package.json @@ -1,43 +1,43 @@ { - "name": "@kwooshung/algorithms-sorts", + "name": "@kwooshung/algorithm-sorts", "version": "0.9.9", - "title": "algorithms-sorts", + "title": "algorithm-sorts", "description": "Better algorithm implementation library", "private": false, "tags": [ "kwooshung", "algorithms", "algorithms.js", - "algorithms-sorts", - "algorithms-sorts.js", + "algorithm-sorts", + "algorithm-sorts.js", "suanfa" ], "keywords": [ "kwooshung", "algorithms", "algorithms.js", - "algorithms-sorts", - "algorithms-sorts.js", + "algorithm-sorts", + "algorithm-sorts.js", "suanfa" ], "author": "kwooshung", - "homepage": "https://github.com/kwooshung/algorithms-sorts", + "homepage": "https://github.com/kwooshung/algorithm-sorts", "repository": { "type": "git", - "url": "https://github.com/kwooshung/algorithms-sorts.git" + "url": "https://github.com/kwooshung/algorithm-sorts.git" }, "bugs": { - "url": "https://github.com/kwooshung/algorithms-sorts/issues" + "url": "https://github.com/kwooshung/algorithm-sorts/issues" }, "type": "module", - "main": "dist/algorithms-sorts.cjs", - "module": "dist/algorithms-sorts.mjs", - "unpkg": "dist/algorithms-sorts.js", + "main": "dist/algorithm-sorts.cjs", + "module": "dist/algorithm-sorts.mjs", + "unpkg": "dist/algorithm-sorts.js", "types": "dist/index.d.ts", "exports": { ".": { - "require": "./dist/algorithms-sorts.cjs", - "import": "./dist/algorithms-sorts.mjs", + "require": "./dist/algorithm-sorts.cjs", + "import": "./dist/algorithm-sorts.mjs", "types": "./dist/index.d.ts" } }, diff --git a/src/compare.cjs b/src/compare.cjs index c05b53b..6aaeb99 100644 --- a/src/compare.cjs +++ b/src/compare.cjs @@ -14,8 +14,9 @@ const { sortBubble, sortShortBubble, sortTim, - sortPancake -} = require('../dist/algorithm.cjs'); + sortPancake, + sortCocktail +} = require('../dist/algorithm-sorts.cjs'); const fns = { /** @@ -179,15 +180,13 @@ const fns = { let oldNoFunction = false; oldStartTime = performance.now(); - if (key === 'counting') { - fns.algorithms.old['counting'](data1); - } else if (key === 'optimizedCounting') { + if (key === 'optimizedCounting') { let inx = 0; let oldCountingEmpty; // 有的时候,老版本不能处理某些随机的数据,不知是什么原因导致最终结果为空数组,这里做一个循环,重新生成数据,直到结果不为空 do { // 首先,排序一次 - oldCountingEmpty = fns.algorithms.old['counting'](data1).length === 0; + oldCountingEmpty = fns.algorithms.old[key](data1).length === 0; // 如果结果为空 if (oldCountingEmpty) { @@ -218,7 +217,7 @@ const fns = { oldExecutionTime = oldEndTime - oldStartTime; const newStartTime = performance.now(); - if (key === 'counting' || key === 'optimizedCounting') { + if (key === 'optimizedCounting') { fns.algorithms.new[key](data2, true); } else if (key === 'radix') { fns.algorithms.new[key]('lsd', data2, true); @@ -237,7 +236,7 @@ const fns = { console.log(`${chalk.dim('├──')} ${chalk.magenta('algorithms.js')} ${chalk.redBright('不存在对应算法 / The corresponding algorithm does not exist.')}`); console.log( - `${chalk.dim('├──')} ${chalk.green('@kwooshung/algorithms')} ${chalk.greenBright(fns.format.decimal(newExecutionTime))} ${chalk.dim('ms')} ${tips.length >= 2 ? tips[1] : ''}` + `${chalk.dim('├──')} ${chalk.green('@kwooshung/algorithm-sorts')} ${chalk.greenBright(fns.format.decimal(newExecutionTime))} ${chalk.dim('ms')} ${tips.length >= 2 ? tips[1] : ''}` ); console.log(`${chalk.dim('└')} ${chalk.bold.greenBright('0')} ${chalk.dim('ms')}\n`); @@ -249,7 +248,7 @@ const fns = { ); console.log( - `${chalk.dim('├──')} ${chalk.green('@kwooshung/algorithms')} ${chalk[isNewFaster ? 'greenBright' : 'redBright'](fns.format.decimal(newExecutionTime))} ${chalk.dim('ms')} ${ + `${chalk.dim('├──')} ${chalk.green('@kwooshung/algorithm-sorts')} ${chalk[isNewFaster ? 'greenBright' : 'redBright'](fns.format.decimal(newExecutionTime))} ${chalk.dim('ms')} ${ tips.length >= 2 ? tips[1] : '' }` ); @@ -276,7 +275,7 @@ const fns = { insertion: algorithmOld.Sorting.insertionSort, selection: algorithmOld.Sorting.selectionSort, shell: algorithmOld.Sorting.shellSort, - counting: algorithmOld.Sorting.countingSort, + optimizedCounting: algorithmOld.Sorting.countingSort, radix: algorithmOld.Sorting.radixSort, bubble: algorithmOld.Sorting.bubbleSort, shortBubble: algorithmOld.Sorting.shortBubbleSort @@ -297,7 +296,8 @@ const fns = { bubble: sortBubble, shortBubble: sortShortBubble, tim: sortTim, - pancake: sortPancake + pancake: sortPancake, + cocktail: sortCocktail } } }; @@ -309,30 +309,11 @@ const fns = { const datas = fns.speeds.datas(count, 6, 32, 0, 1000000); const keyDatas = fns.generate.array(count, 6, 32, 0, 1000000, true); - fns.speeds.test('快速排序 / Quick Sort', 'quick', datas); - fns.speeds.test('归并排序 / Merge Sort', 'merge', datas); - fns.speeds.test('堆排序 / Heap Sort', 'heap', datas); - fns.speeds.test('插入排序 / Insertion Sort', 'insertion', datas); - fns.speeds.test('选择排序 / Selection Sort', 'selection', datas); - fns.speeds.test('希尔排序 / Shell Sort', 'shell', datas); - fns.speeds.test('计数排序 / Counting Sort', 'counting', datas, [ - [ - '计数排序的优化版本 (Optimized version of counting sort)', - '1、适用于数组中的元素是对象,且每个对象有一个名为 key 的整数属性 (Suitable for arrays where elements are objects with an integer attribute named key)', - '2、通过直接使用 key 值作为索引,避免了额外的映射步骤,提高了排序效率 (Uses key values as indices directly, avoiding extra mapping steps and improving efficiency)', - '3、专门针对具有特定结构的数据进行优化 (Specifically optimized for data with certain structures)', - '' - ], - [ - '与之相比 (In comparison)', - '1、本函数为更通用的计数排序实现 (This function is a more general implementation of counting sort)', - '2、由于需要将非数字元素映射为索引,因此效率略低,但它是标准的计数排序 (Efficiency is slightly lower due to the need to map non-numeric elements to indices, However, it is a standard counting sort.)', - '3、适用于更广泛的数据类型,但牺牲了一些效率 (Applicable to a wider range of data types, but at the expense of some efficiency)', - `4、${chalk.magenta('其实这个比较不太公平')},建议参考下一个同类算法的计数排序 (${chalk.magenta( - 'Actually, this comparison is not quite fair' - )}; refer to the counting sort of the next similar algorithm for a better comparison)` - ] - ]); + fns.speeds.test('冒泡排序 / Bubble Sort', 'bubble', datas); + fns.speeds.test('短冒泡排序 / Shout Bubble Sort', 'shortBubble', datas); + fns.speeds.test('鸡尾酒排序 / Cocktail Sort', 'cocktail', datas); + + fns.speeds.test('计数排序 / Counting Sort', 'counting', datas); fns.speeds.test('计数排序(优化版本) / Optimized Counting Sort', 'optimizedCounting', keyDatas, [ [ `有的时候,${chalk.magenta('algorithms.js')} 不能处理某些随机的数据 (Sometimes, ${chalk.magenta('algorithms.js')} can't process certain random data.)`, @@ -342,9 +323,13 @@ const fns = { '' ] ]); + fns.speeds.test('堆排序 / Heap Sort', 'heap', datas); + fns.speeds.test('插入排序 / Insertion Sort', 'insertion', datas); + fns.speeds.test('归并排序 / Merge Sort', 'merge', datas); + fns.speeds.test('煎饼排序 / Pancake Sort', 'pancake', datas); + fns.speeds.test('快速排序 / Quick Sort', 'quick', datas); fns.speeds.test('基数排序 / Radix Sort', 'radix', datas); - fns.speeds.test('冒泡排序 / Bubble Sort', 'bubble', datas); - fns.speeds.test('短冒泡排序 / Shout Bubble Sort', 'shortBubble', datas); + fns.speeds.test('选择排序 / Selection Sort', 'selection', datas); + fns.speeds.test('希尔排序 / Shell Sort', 'shell', datas); fns.speeds.test('定向排序 / Tim Sort', 'tim', datas); - fns.speeds.test('煎饼排序 / Pancake Sort', 'pancake', datas); })(); diff --git a/src/index.ts b/src/index.ts index 7963060..12a0bb9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,13 +1,14 @@ -export { default as sortQuick } from '@/sorts/quick'; -export { default as sortMerge } from '@/sorts/merge'; +export { default as sortBubble } from '@/sorts/bubble'; +export { default as sortShortBubble } from '@/sorts/bubble/short'; +export { default as sortCocktail } from '@/sorts/cocktail'; +export { default as sortCounting } from '@/sorts/counting'; +export { default as sortOptimizedCounting } from '@/sorts/counting/optimized'; export { default as sortHeap } from '@/sorts/heap'; export { default as sortInsertion } from '@/sorts/insertion'; +export { default as sortMerge } from '@/sorts/merge'; +export { default as sortPancake } from '@/sorts/pancake'; +export { default as sortQuick } from '@/sorts/quick'; +export { default as sortRadix } from '@/sorts/radix'; export { default as sortSelection } from '@/sorts/selection'; export { default as sortShell } from '@/sorts/shell'; -export { default as sortCounting } from '@/sorts/counting'; -export { default as sortOptimizedCounting } from '@/sorts/counting/optimized'; -export { default as sortRadix } from '@/sorts/radix'; -export { default as sortBubble } from '@/sorts/bubble'; -export { default as sortShortBubble } from '@/sorts/bubble/short'; export { default as sortTim } from '@/sorts/tim'; -export { default as sortPancake } from '@/sorts/pancake'; diff --git a/src/sorts/cocktail/index.ts b/src/sorts/cocktail/index.ts index 44c526f..502692a 100644 --- a/src/sorts/cocktail/index.ts +++ b/src/sorts/cocktail/index.ts @@ -1,7 +1,7 @@ import compare from '@/internal/compare'; /** - * 鸡尾酒排序(Cocktail Sort)/ Cocktail Sort + * 鸡尾酒排序 / Cocktail Sort * @description 鸡尾酒排序是冒泡排序的变体,其中排序操作是双向的:先从左到右,然后从右到左。/ Cocktail sort is a variation of bubble sort where the sorting operation is bidirectional: first from left to right, then from right to left. * @usageScenario 适用于大部分已排序但部分元素处于错误位置的数组。/ Suitable for mostly sorted arrays with some elements out of place. * @timeComplexity 平均情况和最坏情况均为 O(n^2),最好情况为 O(n) / Average and worst case O(n^2), best case O(n) diff --git a/src/sorts/pancake/index.ts b/src/sorts/pancake/index.ts index 8e97669..09193b9 100644 --- a/src/sorts/pancake/index.ts +++ b/src/sorts/pancake/index.ts @@ -30,7 +30,7 @@ const findMaxIndex = (arr: T[], n: number, comp: compare): number => { }; /** - * 煎饼排序(Pancake Sorting)/ Pancake Sorting + * 煎饼排序 / Pancake Sorting * @description 煎饼排序是一种排序算法,通过一系列的煎饼翻转操作来排序数组。在煎饼排序中,允许的操作是反转数组的前 n 个元素。/ Pancake sorting is a sorting algorithm that sorts an array through a series of pancake flipping operations. In pancake sorting, the allowed operation is flipping the first n elements of the array. * @usageScenario 适用于当翻转操作成本高于比较操作时的排序场景。/ Suitable for sorting scenarios where the cost of flipping operations is higher than comparison operations. * @timeComplexity 平均情况和最坏情况均为 O(n^2),最好情况为 O(n) / Average and worst case O(n^2), best case O(n) diff --git a/vite.config.ts b/vite.config.ts index 45caf96..565cc0a 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -5,6 +5,6 @@ export default defineConfig({ base: './', server: configServer(), plugins: [configDts], - build: configBuild('AlgorithmsSorts', 'algorithms-sorts', ['cjs', 'esm', 'umd']), + build: configBuild('AlgorithmSorts', 'algorithm-sorts', ['cjs', 'esm', 'umd']), resolve: configResolve });