Archive
This commit is contained in:
64
resources/system_design/20260326152929.md
Normal file
64
resources/system_design/20260326152929.md
Normal 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和浏览器,不影响核心逻辑。
|
||||
|
||||
Reference in New Issue
Block a user