1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

doc: update document for algorithm package

This commit is contained in:
dudaodong
2022-12-29 15:32:47 +08:00
parent 39c576248c
commit b5f7b0e670
6 changed files with 88 additions and 33 deletions

View File

@@ -81,19 +81,41 @@ import "github.com/duke-git/lancet/v2/algorithm"
``` ```
#### Function list: #### Function list:
- **<big>BubbleSort</big>** : sorts slice with bubble sort algorithm, will change the original slice.
- [BubbleSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#BubbleSort) [[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#BubbleSort)]
- [CountSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#CountSort) [[play](https://go.dev/play/p/GNdv7Jg2Taj)]
- [HeapSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#HeapSort) - **<big>CountSort</big>** : sorts slice with bubble sort algorithm, don't change original slice.
- [InsertionSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#InsertionSort) [[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#CountSort)]
- [MergeSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#MergeSort) [[play](https://go.dev/play/p/tB-Umgm0DrP)]
- [QuickSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#QuickSort) - **<big>HeapSort</big>** : sorts slice with heap sort algorithm, will change the original slice.
- [SelectionSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#SelectionSort) [[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#HeapSort)]
- [ShellSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#ShellSort) [[play](https://go.dev/play/p/u6Iwa1VZS_f)]
- [BinarySearch](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#BinarySearch) - **<big>InsertionSort</big>** : sorts slice with insertion sort algorithm, will change the original slice.
- [BinaryIterativeSearch](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#BinaryIterativeSearch) [[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#InsertionSort)]
- [LinearSearch](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#LinearSearch) [[play](https://go.dev/play/p/G5LJiWgJJW6)]
- [LRUCache](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#LRUCache) - **<big>MergeSort</big>** : sorts slice with merge sort algorithm, will change the original slice.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#MergeSort)]
[[play](https://go.dev/play/p/ydinn9YzUJn)]
- **<big>QuickSort</big>** : sorts slice with quick sort algorithm, will change the original slice.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#QuickSort)]
[[play](https://go.dev/play/p/7Y7c1Elk3ax)]
- **<big>SelectionSort</big>** : sorts slice with selection sort algorithm, will change the original slice.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#SelectionSort)]
[[play](https://go.dev/play/p/oXovbkekayS)]
- **<big>ShellSort</big>** : sorts slice with shell sort algorithm, will change the original slice.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#ShellSort)]
[[play](https://go.dev/play/p/3ibkszpJEu3)]
- **<big>BinarySearch</big>** : returns the index of target within a sorted slice, use binary search (recursive call itself).
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#BinarySearch)]
[[play](https://go.dev/play/p/t6MeGiUSN47)]
- **<big>BinaryIterativeSearch</big>** : returns the index of target within a sorted slice, use binary search (no recursive).
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#BinaryIterativeSearch)]
[[play](https://go.dev/play/p/Anozfr8ZLH3)]
- **<big>LinearSearch</big>** : returns the index of target in slice base on equal function.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#LinearSearch)]
- **<big>LRUCache</big>** : implements memory cache with lru algorithm.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#LRUCache)]
[[play](https://go.dev/play/p/-EZjgOURufP)]
### 2. Concurrency package contain some functions to support concurrent programming. eg, goroutine, channel, async. ### 2. Concurrency package contain some functions to support concurrent programming. eg, goroutine, channel, async.

View File

@@ -79,19 +79,41 @@ import "github.com/duke-git/lancet/v2/algorithm"
``` ```
#### Function list: #### Function list:
- **<big>BubbleSort</big>** : 使用冒泡排序算法对切片进行排序。
- [BubbleSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#BubbleSort) [[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#BubbleSort)]
- [CountSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#CountSort) [[play](https://go.dev/play/p/GNdv7Jg2Taj)]
- [HeapSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#HeapSort) - **<big>CountSort</big>** : 使用计数排序算法对切片进行排序。不改变原数据。
- [InsertionSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#InsertionSort) [[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#CountSort)]
- [MergeSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#MergeSort) [[play](https://go.dev/play/p/tB-Umgm0DrP)]
- [QuickSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#QuickSort) - **<big>HeapSort</big>** : 使用堆排序算法对切片进行排序。
- [SelectionSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#SelectionSort) [[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#HeapSort)]
- [ShellSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#ShellSort) [[play](https://go.dev/play/p/u6Iwa1VZS_f)]
- [BinarySearch](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#BinarySearch) - **<big>InsertionSort</big>** : 使用插入排序算法对切片进行排序。
- [BinaryIterativeSearch](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#BinaryIterativeSearch) [[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#InsertionSort)]
- [LinearSearch](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LinearSearch) [[play](https://go.dev/play/p/G5LJiWgJJW6)]
- [LRUCache](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LRUCache) - **<big>MergeSort</big>** : 使用合并排序算法对切片进行排序。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#MergeSort)]
[[play](https://go.dev/play/p/ydinn9YzUJn)]
- **<big>QuickSort</big>** : 使用快速排序算法对切片进行排序。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#QuickSort)]
[[play](https://go.dev/play/p/7Y7c1Elk3ax)]
- **<big>SelectionSort</big>** : 使用选择排序算法对切片进行排序。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#SelectionSort)]
[[play](https://go.dev/play/p/oXovbkekayS)]
- **<big>ShellSort</big>** : 使用希尔排序算法对切片进行排序。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#ShellSort)]
[[play](https://go.dev/play/p/3ibkszpJEu3)]
- **<big>BinarySearch</big>** : 返回排序切片中目标值的索引,使用二分搜索(递归调用)。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#BinarySearch)]
[[play](https://go.dev/play/p/t6MeGiUSN47)]
- **<big>BinaryIterativeSearch</big>** :返回排序切片中目标值的索引,使用二分搜索(非递归)。
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#BinaryIterativeSearch)]
[[play](https://go.dev/play/p/Anozfr8ZLH3)]
- **<big>LinearSearch</big>** : 基于传入的相等函数返回切片中目标值的索引。(线性查找)
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LinearSearch)]
- **<big>LRUCache</big>** : 应用lru算法实现内存缓存.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LRUCache)]
[[play](https://go.dev/play/p/-EZjgOURufP)]
### 2. concurrency包含一些支持并发编程的功能。例如goroutine, channel, async 等。 ### 2. concurrency包含一些支持并发编程的功能。例如goroutine, channel, async 等。

View File

@@ -1,7 +1,7 @@
// Copyright 2021 dudaodong@gmail.com. All rights reserved. // Copyright 2021 dudaodong@gmail.com. All rights reserved.
// Use of this source code is governed by MIT license // Use of this source code is governed by MIT license
// Package algorithm contain some basic algorithm functions. eg. sort, search, list, linklist, stack, queue, tree, graph. TODO // Package algorithm contain some basic algorithm functions. eg. sort, search, list, linklist, stack, queue, tree, graph.
package algorithm package algorithm
import "github.com/duke-git/lancet/v2/lancetconstraints" import "github.com/duke-git/lancet/v2/lancetconstraints"

View File

@@ -1,7 +1,6 @@
// Copyright 2021 dudaodong@gmail.com. All rights reserved. // Copyright 2021 dudaodong@gmail.com. All rights reserved.
// Use of this source code is governed by MIT license // Use of this source code is governed by MIT license
// Package algorithm contain some basic algorithm functions. eg. sort, search
package algorithm package algorithm
import "github.com/duke-git/lancet/v2/lancetconstraints" import "github.com/duke-git/lancet/v2/lancetconstraints"
@@ -35,7 +34,7 @@ func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) {
} }
// SelectionSort applys the selection sort algorithm to sort the collection, will change the original collection data. // SelectionSort applys the selection sort algorithm to sort the collection, will change the original collection data.
// Play: // Play: https://go.dev/play/p/oXovbkekayS
func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) { func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) {
for i := 0; i < len(slice); i++ { for i := 0; i < len(slice); i++ {
min := i min := i

View File

@@ -570,6 +570,8 @@ func main() {
func NewLRUCache[K comparable, V any](capacity int) *LRUCache[K, V] func NewLRUCache[K comparable, V any](capacity int) *LRUCache[K, V]
func (l *LRUCache[K, V]) Get(key K) (V, bool) func (l *LRUCache[K, V]) Get(key K) (V, bool)
func (l *LRUCache[K, V]) Put(key K, value V) func (l *LRUCache[K, V]) Put(key K, value V)
func (l *LRUCache[K, V]) Delete(key K) bool
func (l *LRUCache[K, V]) Len() int
``` ```
<b>Example:</b> <b>Example:</b>
@@ -586,10 +588,14 @@ func main() {
cache.Put(1, 1) cache.Put(1, 1)
cache.Put(2, 2) cache.Put(2, 2)
cache.Put(3, 3)
_, ok := cache.Get(0) // ok -> false fmt.Println(cache.Len()) // 3
v, ok := cache.Get(1) // v->1, ok->true v, ok := cache.Get(1)
fmt.Println(v, ok) // 1 true
ok = cache.Delete(1)
fmt.Println(ok) // true
} }
``` ```

View File

@@ -570,6 +570,8 @@ func main() {
func NewLRUCache[K comparable, V any](capacity int) *LRUCache[K, V] func NewLRUCache[K comparable, V any](capacity int) *LRUCache[K, V]
func (l *LRUCache[K, V]) Get(key K) (V, bool) func (l *LRUCache[K, V]) Get(key K) (V, bool)
func (l *LRUCache[K, V]) Put(key K, value V) func (l *LRUCache[K, V]) Put(key K, value V)
func (l *LRUCache[K, V]) Delete(key K) bool
func (l *LRUCache[K, V]) Len() int
``` ```
<b>Example:</b> <b>Example:</b>
@@ -586,10 +588,14 @@ func main() {
cache.Put(1, 1) cache.Put(1, 1)
cache.Put(2, 2) cache.Put(2, 2)
cache.Put(3, 3)
_, ok := cache.Get(0) // ok -> false fmt.Println(cache.Len()) // 3
v, ok := cache.Get(1) // v->1, ok->true v, ok := cache.Get(1)
fmt.Println(v, ok) // 1 true
ok = cache.Delete(1)
fmt.Println(ok) // true
} }
``` ```