1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-16 18:52:27 +08:00

doc: format document

This commit is contained in:
dudaodong
2023-01-13 10:39:39 +08:00
parent 0c62d117a1
commit c875a7f8b8
2 changed files with 46 additions and 22 deletions

View File

@@ -148,7 +148,7 @@ func main() {
fmt.Println(peoples) fmt.Println(peoples)
// Output: // Output:
//[{d 8} {b 10} {c 17} {a 20} {e 28}] // [{d 8} {b 10} {c 17} {a 20} {e 28}]
} }
``` ```
@@ -196,7 +196,7 @@ func main() {
fmt.Println(numbers) fmt.Println(numbers)
// Output: // Output:
// [1 2 3 4 5 6] // [1 2 3 4 5 6]
} }
``` ```

View File

@@ -1,17 +1,19 @@
# Algorithm # Algorithm
algorithm算法包实现一些基本算法sortsearchlrucache。
algorithm 算法包实现一些基本算法sortsearchlrucache。
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 源码 ## 源码
- [https://github.com/duke-git/lancet/blob/main/algorithm/sort.go](https://github.com/duke-git/lancet/blob/main/algorithm/sort.go) - [https://github.com/duke-git/lancet/blob/main/algorithm/sort.go](https://github.com/duke-git/lancet/blob/main/algorithm/sort.go)
- [https://github.com/duke-git/lancet/blob/main/algorithm/search.go](https://github.com/duke-git/lancet/blob/main/algorithm/search.go) - [https://github.com/duke-git/lancet/blob/main/algorithm/search.go](https://github.com/duke-git/lancet/blob/main/algorithm/search.go)
- [https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.go](https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.go) - [https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.go](https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.go)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 用法 ## 用法
```go ```go
import ( import (
"github.com/duke-git/lancet/v2/algorithm" "github.com/duke-git/lancet/v2/algorithm"
@@ -22,25 +24,25 @@ import (
## 目录 ## 目录
- [BubbleSort](#BubbleSort) - [BubbleSort](#BubbleSort)
- [InsertionSort](#InsertionSort) - [InsertionSort](#InsertionSort)
- [SelectionSort](#SelectionSort) - [SelectionSort](#SelectionSort)
- [ShellSort](#ShellSort) - [ShellSort](#ShellSort)
- [QuickSort](#QuickSort) - [QuickSort](#QuickSort)
- [HeapSort](#HeapSort) - [HeapSort](#HeapSort)
- [MergeSort](#MergeSort) - [MergeSort](#MergeSort)
- [CountSort](#CountSort) - [CountSort](#CountSort)
- [BinarySearch](#BinarySearch) - [BinarySearch](#BinarySearch)
- [BinaryIterativeSearch](#BinaryIterativeSearch) - [BinaryIterativeSearch](#BinaryIterativeSearch)
- [LinearSearch](#LinearSearch) - [LinearSearch](#LinearSearch)
- [LRUCache](#LRUCache) - [LRUCache](#LRUCache)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 文档 ## 文档
### <span id="BubbleSort">BubbleSort</span> ### <span id="BubbleSort">BubbleSort</span>
<p>冒泡排序参数comparator需要实现包lancetconstraints.Comparator。</p> <p>冒泡排序参数comparator需要实现包lancetconstraints.Comparator。</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -48,6 +50,7 @@ import (
```go ```go
func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator) func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator)
``` ```
<b>示例:</b> <b>示例:</b>
```go ```go
@@ -87,6 +90,7 @@ func main() {
``` ```
### <span id="InsertionSort">InsertionSort</span> ### <span id="InsertionSort">InsertionSort</span>
<p>插入排序参数comparator需要实现包lancetconstraints.Comparator。</p> <p>插入排序参数comparator需要实现包lancetconstraints.Comparator。</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -94,6 +98,7 @@ func main() {
```go ```go
func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator)
``` ```
<b>示例:</b> <b>示例:</b>
```go ```go
@@ -141,14 +146,14 @@ func main() {
algorithm.InsertionSort(peoples, comparator) algorithm.InsertionSort(peoples, comparator)
fmt.Println(peoples) fmt.Println(peoples)
// Output: // Output:
//[{d 8} {b 10} {c 17} {a 20} {e 28}] //[{d 8} {b 10} {c 17} {a 20} {e 28}]
} }
``` ```
### <span id="SelectionSort">SelectionSort</span> ### <span id="SelectionSort">SelectionSort</span>
<p>选择排序参数comparator需要实现包lancetconstraints.Comparator。</p> <p>选择排序参数comparator需要实现包lancetconstraints.Comparator。</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -156,6 +161,7 @@ func main() {
```go ```go
func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator)
``` ```
<b>示例:</b> <b>示例:</b>
```go ```go
@@ -195,6 +201,7 @@ func main() {
``` ```
### <span id="ShellSort">ShellSort</span> ### <span id="ShellSort">ShellSort</span>
<p>希尔排序参数comparator需要实现包lancetconstraints.Comparator。</p> <p>希尔排序参数comparator需要实现包lancetconstraints.Comparator。</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -202,6 +209,7 @@ func main() {
```go ```go
func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator) func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator)
``` ```
<b>示例:</b> <b>示例:</b>
```go ```go
@@ -241,6 +249,7 @@ func main() {
``` ```
### <span id="QuickSort">QuickSort</span> ### <span id="QuickSort">QuickSort</span>
<p>快速排序参数comparator需要实现包lancetconstraints.Comparator。</p> <p>快速排序参数comparator需要实现包lancetconstraints.Comparator。</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -248,6 +257,7 @@ func main() {
```go ```go
func QuickSort[T any](slice []T comparator lancetconstraints.Comparator) func QuickSort[T any](slice []T comparator lancetconstraints.Comparator)
``` ```
<b>示例:</b> <b>示例:</b>
```go ```go
@@ -287,6 +297,7 @@ func main() {
``` ```
### <span id="HeapSort">HeapSort</span> ### <span id="HeapSort">HeapSort</span>
<p>堆排序参数comparator需要实现包lancetconstraints.Comparator。</p> <p>堆排序参数comparator需要实现包lancetconstraints.Comparator。</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -294,6 +305,7 @@ func main() {
```go ```go
func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator) func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator)
``` ```
<b>示例:</b> <b>示例:</b>
```go ```go
@@ -333,6 +345,7 @@ func main() {
``` ```
### <span id="MergeSort">MergeSort</span> ### <span id="MergeSort">MergeSort</span>
<p>归并排序参数comparator需要实现包lancetconstraints.Comparator。</p> <p>归并排序参数comparator需要实现包lancetconstraints.Comparator。</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -340,6 +353,7 @@ func main() {
```go ```go
func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator) func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator)
``` ```
<b>示例:</b> <b>示例:</b>
```go ```go
@@ -379,6 +393,7 @@ func main() {
``` ```
### <span id="CountSort">CountSort</span> ### <span id="CountSort">CountSort</span>
<p>计数排序参数comparator需要实现包lancetconstraints.Comparator。</p> <p>计数排序参数comparator需要实现包lancetconstraints.Comparator。</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -386,6 +401,7 @@ func main() {
```go ```go
func CountSort[T any](slice []T, comparator lancetconstraints.Comparator) []T func CountSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
``` ```
<b>示例:</b> <b>示例:</b>
```go ```go
@@ -426,6 +442,7 @@ func main() {
``` ```
### <span id="BinarySearch">BinarySearch</span> ### <span id="BinarySearch">BinarySearch</span>
<p>二分递归查找,返回元素索引,未找到元素返回-1参数comparator需要实现包lancetconstraints.Comparator。</p> <p>二分递归查找,返回元素索引,未找到元素返回-1参数comparator需要实现包lancetconstraints.Comparator。</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -433,6 +450,7 @@ func main() {
```go ```go
func BinarySearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int func BinarySearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int
``` ```
<b>示例:</b> <b>示例:</b>
```go ```go
@@ -475,6 +493,7 @@ func main() {
``` ```
### <span id="BinaryIterativeSearch">BinaryIterativeSearch</span> ### <span id="BinaryIterativeSearch">BinaryIterativeSearch</span>
<p>二分迭代查找,返回元素索引,未找到元素返回-1参数comparator需要实现包lancetconstraints.Comparator。</p> <p>二分迭代查找,返回元素索引,未找到元素返回-1参数comparator需要实现包lancetconstraints.Comparator。</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -482,6 +501,7 @@ func main() {
```go ```go
func BinaryIterativeSearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int func BinaryIterativeSearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int
``` ```
<b>示例:</b> <b>示例:</b>
```go ```go
@@ -524,6 +544,7 @@ func main() {
``` ```
### <span id="LinearSearch">LinearSearch</span> ### <span id="LinearSearch">LinearSearch</span>
<p>基于传入的相等函数线性查找元素,返回元素索引,未找到元素返回-1。</p> <p>基于传入的相等函数线性查找元素,返回元素索引,未找到元素返回-1。</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -531,6 +552,7 @@ func main() {
```go ```go
func LinearSearch[T any](slice []T, target T, equal func(a, b T) bool) int func LinearSearch[T any](slice []T, target T, equal func(a, b T) bool) int
``` ```
<b>示例:</b> <b>示例:</b>
```go ```go
@@ -561,6 +583,7 @@ func main() {
``` ```
### <span id="LRUCache">LRUCache</span> ### <span id="LRUCache">LRUCache</span>
<p>lru算法实现缓存。</p> <p>lru算法实现缓存。</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -572,6 +595,7 @@ func (l *LRUCache[K, V]) Put(key K, value V)
func (l *LRUCache[K, V]) Delete(key K) bool func (l *LRUCache[K, V]) Delete(key K) bool
func (l *LRUCache[K, V]) Len() int func (l *LRUCache[K, V]) Len() int
``` ```
<b>示例:</b> <b>示例:</b>
```go ```go
@@ -609,4 +633,4 @@ func main() {
// 2 // 2
// true // true
} }
``` ```