This commit is contained in:
ubuntu
2026-03-26 15:33:47 +09:00
parent fff89a2d9b
commit 69253957ee
13 changed files with 357 additions and 0 deletions

View File

@@ -0,0 +1,64 @@
## Implementation approach
实现一个纯JavaScript的快速排序算法无需任何第三方依赖库。采用经典的分治策略选取基准元素pivot将数组分为小于、等于、大于基准的三个部分递归排序后合并。同时支持升序/降序参数处理边界情况空数组、单元素数组并通过console.log提供示例验证。代码结构简单仅需两个文件核心排序模块和入口示例文件。
## File list
- index.js
- quickSort.js
## Data structures and interfaces
classDiagram
class QuickSort {
+quickSort(arr: number[], descending: boolean) number[]
-partition(arr: number[], descending: boolean) object
}
class Main {
+run() void
}
Main --> QuickSort : uses
## Program call flow
sequenceDiagram
participant M as Main (index.js)
participant QS as QuickSort (quickSort.js)
M->>M: 定义测试数组 testArrays
M->>QS: quickSort([], false)
QS-->>M: 返回 []
M->>M: console.log 输出空数组结果
M->>QS: quickSort([42], false)
QS-->>M: 返回 [42]
M->>M: console.log 输出单元素结果
M->>QS: quickSort([3,6,8,10,1,2,1], false)
QS->>QS: 选取 pivot = arr[middle]
QS->>QS: 分区为 left, middle, right
QS->>QS: quickSort(left, false) 递归
QS->>QS: quickSort(right, false) 递归
QS-->>M: 返回升序排序结果
M->>M: console.log 输出升序结果
M->>QS: quickSort([3,6,8,10,1,2,1], true)
QS->>QS: 选取 pivot = arr[middle]
QS->>QS: 分区为 left(>pivot), middle, right(<pivot)
QS->>QS: quickSort(left, true) 递归
QS->>QS: quickSort(right, true) 递归
QS-->>M: 返回降序排序结果
M->>M: console.log 输出降序结果
M->>QS: quickSort([1,2,3,4,5], false)
QS-->>M: 返回已排序数组
M->>M: console.log 输出已排序数组结果
## Anything UNCLEAR
1) 降序排序参数已作为可选功能P2纳入实现默认为升序2) 非数字元素过滤未在需求中明确要求本实现假设输入均为数字数组3) 运行环境兼容Node.js和浏览器不影响核心逻辑。