1 Star 0 Fork 0

KwooShung / AlgorithmSorts

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

@kwooshung/algorithm-sorts

GitHub Release Date - Published_At GitHub last commit GitHub code size in bytes GitHub top language GitHub pull requests GitHub issues NPM Version Npm.js Downloads/Week Github CI/CD codecov Maintainability GitHub License Gitee Repo Github Stars

English | 中文

为什么开发它?

  • 开发时,我需要用到算法,我找到了 algorithms.js 这个库,它的实现方式非常好,本项目的 compare 就在 algorithms.js 的基础略作修改。
  • algorithms.js 不是通过 esm 的规范实现的,而是通过 commonjs 的规范实现的,这样就不能使用 树摇(tree-shaking) 了,虽然有一些间接的方式实现,但不如直接使用 esm规范来的方便。
  • algorithms.js 已有多年没有维护了,一些算法和实现逻辑使用现代的方式实现会更好。

为什么使用它?

  • Typescript 编写,并进行了单元测试,保证了代码质量,代码覆盖率达到 100%
  • 支持 esmcommonjs 规范,esm 规范可以直接使用 树摇(tree-shaking),减少打包体积;
  • 清晰的注释,包含中英文注释,方便阅读和理解,如下所示;
/**
 * 短冒泡排序 / 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 多了几个,后续会继续添加;
  • 大部分情况下,本库算法效率优于 algorithms.js,下方有对比图表,您也可以 git clone 本项目,运行 npm compare 进行测试;

什么时候不要使用它?

就如项目名称而言,你也应该知道,本项目的算法只支持 排序,如果你需要其他算法,可以使用 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

npm install @kwooshung/algorithm-sorts

yarn

yarn add @kwooshung/algorithm-sorts

pnpm

pnpm add @kwooshung/algorithm-sorts

使用方法

引入

esm

import { bubbleSort } from '@kwooshung/algorithm-sorts';

commonjs

const { bubbleSort } = require('@kwooshung/algorithm-sorts');

已支持的算法

目前已支持如下排序算法,具体使用方法,点击下方排序名称,参考注释和参数即可:

Gitee 仓库

KwooShung/algorithm-sorts

MIT License Copyright (c) 2024 KwooShung Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

A better algorithm sorts implementation library 展开 收起
TypeScript 等 3 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/kwooshung/AlgorithmSorts.git
git@gitee.com:kwooshung/AlgorithmSorts.git
kwooshung
AlgorithmSorts
AlgorithmSorts
main

搜索帮助

53164aa7 5694891 3bd8fe86 5694891