diff --git a/docs/algorithm.md b/docs/algorithm.md index ec30809..bd34114 100644 --- a/docs/algorithm.md +++ b/docs/algorithm.md @@ -1,17 +1,19 @@ # Algorithm + Package algorithm implements some basic algorithm. eg. sort, search.
## Source -- [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/lru_cache.go](https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.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/lru_cache.go](https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.go) ## Usage + ```go import ( "github.com/duke-git/lancet/v2/algorithm" @@ -22,25 +24,25 @@ import ( ## Index -- [BubbleSort](#BubbleSort) -- [InsertionSort](#InsertionSort) -- [SelectionSort](#SelectionSort) -- [ShellSort](#ShellSort) -- [QuickSort](#QuickSort) -- [HeapSort](#HeapSort) -- [MergeSort](#MergeSort) -- [CountSort](#CountSort) -- [BinarySearch](#BinarySearch) -- [BinaryIterativeSearch](#BinaryIterativeSearch) -- [LinearSearch](#LinearSearch) -- [LRUCache](#LRUCache) - +- [BubbleSort](#BubbleSort) +- [InsertionSort](#InsertionSort) +- [SelectionSort](#SelectionSort) +- [ShellSort](#ShellSort) +- [QuickSort](#QuickSort) +- [HeapSort](#HeapSort) +- [MergeSort](#MergeSort) +- [CountSort](#CountSort) +- [BinarySearch](#BinarySearch) +- [BinaryIterativeSearch](#BinaryIterativeSearch) +- [LinearSearch](#LinearSearch) +- [LRUCache](#LRUCache) ## Documentation ### BubbleSort +Sort slice with bubble sort algorithm. Param comparator should implements lancetconstraints.Comparator.
Signature: @@ -48,6 +50,7 @@ import ( ```go func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator) ``` + Example: ```go @@ -87,6 +90,7 @@ func main() { ``` ### InsertionSort +Sort slice with insertion sort algorithm. Param comparator should implements lancetconstraints.Comparator.
Signature: @@ -94,6 +98,7 @@ func main() { ```go func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) ``` + Example: ```go @@ -141,14 +146,14 @@ func main() { algorithm.InsertionSort(peoples, comparator) fmt.Println(peoples) - + // Output: //[{d 8} {b 10} {c 17} {a 20} {e 28}] } ``` - ### SelectionSort +Sort slice with selection sort algorithm. Param comparator should implements lancetconstraints.Comparator.
Signature: @@ -156,6 +161,7 @@ func main() { ```go func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) ``` + Example: ```go @@ -195,6 +201,7 @@ func main() { ``` ### ShellSort +Sort slice with shell sort algorithm. Param comparator should implements lancetconstraints.Comparator.
Signature: @@ -202,6 +209,7 @@ func main() { ```go func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator) ``` + Example: ```go @@ -241,6 +249,7 @@ func main() { ``` ### QuickSort +Sort slice with quick sort algorithm. Param comparator should implements lancetconstraints.Comparator.
Signature: @@ -248,6 +257,7 @@ func main() { ```go func QuickSort[T any](slice []T comparator lancetconstraints.Comparator) ``` + Example: ```go @@ -287,6 +297,7 @@ func main() { ``` ### HeapSort +Sort slice with heap sort algorithm. Param comparator should implements lancetconstraints.Comparator.
Signature: @@ -294,6 +305,7 @@ func main() { ```go func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator) ``` + Example: ```go @@ -333,6 +345,7 @@ func main() { ``` ### MergeSort +Sort slice with merge sort algorithm. Param comparator should implements lancetconstraints.Comparator.
Signature: @@ -340,6 +353,7 @@ func main() { ```go func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator) ``` + Example: ```go @@ -379,6 +393,7 @@ func main() { ``` ### CountSort +Sort slice with count sort algorithm. Param comparator should implements lancetconstraints.Comparator.
Signature: @@ -386,6 +401,7 @@ func main() { ```go func CountSort[T any](slice []T, comparator lancetconstraints.Comparator) []T ``` + Example: ```go @@ -426,6 +442,7 @@ func main() { ``` ### BinarySearch +BinarySearch search for target within a sorted slice, recursive call itself. If a target is found, the index of the target is returned. Else the function return -1.
Signature: @@ -433,6 +450,7 @@ func main() { ```go func BinarySearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int ``` + Example: ```go @@ -475,6 +493,7 @@ func main() { ``` ### BinaryIterativeSearch +BinaryIterativeSearch search for target within a sorted slice, recursive call itself. If a target is found, the index of the target is returned. Else the function return -1.
Signature: @@ -482,6 +501,7 @@ func main() { ```go func BinaryIterativeSearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int ``` + Example: ```go @@ -524,6 +544,7 @@ func main() { ``` ### LinearSearch +return the index of target in slice base on equal function.If a target is found, the index of the target is returned. Else the function return -1.
Signature: @@ -531,6 +552,7 @@ func main() { ```go func LinearSearch[T any](slice []T, target T, equal func(a, b T) bool) int ``` + Example: ```go @@ -561,6 +583,7 @@ func main() { ``` ### LRUCache +LRUCache implements mem cache with lru.
Signature: @@ -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]) Len() int ``` + Example: ```go @@ -609,4 +633,4 @@ func main() { // 2 // true } -``` \ No newline at end of file +```