mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
docs: add docs/algorithm.md
This commit is contained in:
20
README.md
20
README.md
@@ -62,6 +62,26 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
## API Documentation
|
## API Documentation
|
||||||
|
|
||||||
|
### Algorithm package implements some basic algorithm. eg. sort, search.
|
||||||
|
|
||||||
|
```go
|
||||||
|
import "github.com/duke-git/lancet/algorithm"
|
||||||
|
```
|
||||||
|
#### Function list:
|
||||||
|
- [BubbleSort](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)
|
||||||
|
- [InsertionSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#InsertionSort)
|
||||||
|
- [MergeSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#MergeSort)
|
||||||
|
- [QuickSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#QuickSort)
|
||||||
|
- [SelectionSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#SelectionSort)
|
||||||
|
- [ShellSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#ShellSort)
|
||||||
|
- [BinarySearch](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#BinarySearch)
|
||||||
|
- [BinaryIterativeSearch](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#BinaryIterativeSearch)
|
||||||
|
- [LinearSearch](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#LinearSearch)
|
||||||
|
- [LRUCache](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#LRUCache)
|
||||||
|
|
||||||
|
|
||||||
### Convertor package contains some functions for data convertion.
|
### Convertor package contains some functions for data convertion.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
|||||||
@@ -62,6 +62,25 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
## API文档
|
## API文档
|
||||||
|
|
||||||
|
### algorithm算法包实现一些基本算法。eg. sort, search.
|
||||||
|
|
||||||
|
```go
|
||||||
|
import "github.com/duke-git/lancet/algorithm"
|
||||||
|
```
|
||||||
|
#### Function list:
|
||||||
|
- [BubbleSort](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)
|
||||||
|
- [InsertionSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#InsertionSort)
|
||||||
|
- [MergeSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#MergeSort)
|
||||||
|
- [QuickSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#QuickSort)
|
||||||
|
- [SelectionSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#SelectionSort)
|
||||||
|
- [ShellSort](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#ShellSort)
|
||||||
|
- [BinarySearch](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#BinarySearch)
|
||||||
|
- [BinaryIterativeSearch](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#BinaryIterativeSearch)
|
||||||
|
- [LinearSearch](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LinearSearch)
|
||||||
|
- [LRUCache](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LRUCache)
|
||||||
|
|
||||||
### convertor转换器包支持一些常见的数据类型转换。
|
### convertor转换器包支持一些常见的数据类型转换。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
|||||||
@@ -14,10 +14,10 @@ type people struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// PeopleAageComparator sort people slice by age field
|
// PeopleAageComparator sort people slice by age field
|
||||||
type peopleAageComparator struct{}
|
type peopleAgeComparator struct{}
|
||||||
|
|
||||||
// Compare implements github.com/duke-git/lancet/lancetconstraints/constraints.go/Comparator
|
// Compare implements github.com/duke-git/lancet/lancetconstraints/constraints.go/Comparator
|
||||||
func (pc *peopleAageComparator) Compare(v1 interface{}, v2 interface{}) int {
|
func (pc *peopleAgeComparator) Compare(v1 interface{}, v2 interface{}) int {
|
||||||
p1, _ := v1.(people)
|
p1, _ := v1.(people)
|
||||||
p2, _ := v2.(people)
|
p2, _ := v2.(people)
|
||||||
|
|
||||||
@@ -65,7 +65,7 @@ var intSlice = []int{2, 1, 5, 3, 6, 4}
|
|||||||
func TestBubbleSortForStructSlice(t *testing.T) {
|
func TestBubbleSortForStructSlice(t *testing.T) {
|
||||||
asssert := internal.NewAssert(t, "TestBubbleSortForStructSlice")
|
asssert := internal.NewAssert(t, "TestBubbleSortForStructSlice")
|
||||||
|
|
||||||
comparator := &peopleAageComparator{}
|
comparator := &peopleAgeComparator{}
|
||||||
sortedPeopleByAge := BubbleSort(peoples, comparator)
|
sortedPeopleByAge := BubbleSort(peoples, comparator)
|
||||||
t.Log(sortedPeopleByAge)
|
t.Log(sortedPeopleByAge)
|
||||||
|
|
||||||
@@ -88,7 +88,7 @@ func TestBubbleSortForIntSlice(t *testing.T) {
|
|||||||
func TestInsertionSort(t *testing.T) {
|
func TestInsertionSort(t *testing.T) {
|
||||||
asssert := internal.NewAssert(t, "TestInsertionSort")
|
asssert := internal.NewAssert(t, "TestInsertionSort")
|
||||||
|
|
||||||
comparator := &peopleAageComparator{}
|
comparator := &peopleAgeComparator{}
|
||||||
sortedPeopleByAge := SelectionSort(peoples, comparator)
|
sortedPeopleByAge := SelectionSort(peoples, comparator)
|
||||||
t.Log(sortedPeopleByAge)
|
t.Log(sortedPeopleByAge)
|
||||||
|
|
||||||
@@ -101,7 +101,7 @@ func TestInsertionSort(t *testing.T) {
|
|||||||
func TestSelectionSort(t *testing.T) {
|
func TestSelectionSort(t *testing.T) {
|
||||||
asssert := internal.NewAssert(t, "TestSelectionSort")
|
asssert := internal.NewAssert(t, "TestSelectionSort")
|
||||||
|
|
||||||
comparator := &peopleAageComparator{}
|
comparator := &peopleAgeComparator{}
|
||||||
sortedPeopleByAge := SelectionSort(peoples, comparator)
|
sortedPeopleByAge := SelectionSort(peoples, comparator)
|
||||||
t.Log(sortedPeopleByAge)
|
t.Log(sortedPeopleByAge)
|
||||||
|
|
||||||
@@ -114,7 +114,7 @@ func TestSelectionSort(t *testing.T) {
|
|||||||
func TestShellSort(t *testing.T) {
|
func TestShellSort(t *testing.T) {
|
||||||
asssert := internal.NewAssert(t, "TestShellSort")
|
asssert := internal.NewAssert(t, "TestShellSort")
|
||||||
|
|
||||||
comparator := &peopleAageComparator{}
|
comparator := &peopleAgeComparator{}
|
||||||
sortedPeopleByAge := ShellSort(peoples, comparator)
|
sortedPeopleByAge := ShellSort(peoples, comparator)
|
||||||
t.Log(sortedPeopleByAge)
|
t.Log(sortedPeopleByAge)
|
||||||
|
|
||||||
@@ -127,7 +127,7 @@ func TestShellSort(t *testing.T) {
|
|||||||
func TestQuickSort(t *testing.T) {
|
func TestQuickSort(t *testing.T) {
|
||||||
asssert := internal.NewAssert(t, "TestQuickSort")
|
asssert := internal.NewAssert(t, "TestQuickSort")
|
||||||
|
|
||||||
comparator := &peopleAageComparator{}
|
comparator := &peopleAgeComparator{}
|
||||||
sortedPeopleByAge := QuickSort(peoples, 0, len(peoples)-1, comparator)
|
sortedPeopleByAge := QuickSort(peoples, 0, len(peoples)-1, comparator)
|
||||||
t.Log(sortedPeopleByAge)
|
t.Log(sortedPeopleByAge)
|
||||||
|
|
||||||
@@ -140,7 +140,7 @@ func TestQuickSort(t *testing.T) {
|
|||||||
func TestHeapSort(t *testing.T) {
|
func TestHeapSort(t *testing.T) {
|
||||||
asssert := internal.NewAssert(t, "TestHeapSort")
|
asssert := internal.NewAssert(t, "TestHeapSort")
|
||||||
|
|
||||||
comparator := &peopleAageComparator{}
|
comparator := &peopleAgeComparator{}
|
||||||
sortedPeopleByAge := HeapSort(peoples, comparator)
|
sortedPeopleByAge := HeapSort(peoples, comparator)
|
||||||
t.Log(sortedPeopleByAge)
|
t.Log(sortedPeopleByAge)
|
||||||
|
|
||||||
@@ -153,7 +153,7 @@ func TestHeapSort(t *testing.T) {
|
|||||||
func TestMergeSort(t *testing.T) {
|
func TestMergeSort(t *testing.T) {
|
||||||
asssert := internal.NewAssert(t, "TestMergeSort")
|
asssert := internal.NewAssert(t, "TestMergeSort")
|
||||||
|
|
||||||
comparator := &peopleAageComparator{}
|
comparator := &peopleAgeComparator{}
|
||||||
sortedPeopleByAge := MergeSort(peoples, 0, len(peoples)-1, comparator)
|
sortedPeopleByAge := MergeSort(peoples, 0, len(peoples)-1, comparator)
|
||||||
t.Log(sortedPeopleByAge)
|
t.Log(sortedPeopleByAge)
|
||||||
|
|
||||||
@@ -166,7 +166,7 @@ func TestMergeSort(t *testing.T) {
|
|||||||
func TestCountSort(t *testing.T) {
|
func TestCountSort(t *testing.T) {
|
||||||
asssert := internal.NewAssert(t, "TestCountSort")
|
asssert := internal.NewAssert(t, "TestCountSort")
|
||||||
|
|
||||||
comparator := &peopleAageComparator{}
|
comparator := &peopleAgeComparator{}
|
||||||
sortedPeopleByAge := CountSort(peoples, comparator)
|
sortedPeopleByAge := CountSort(peoples, comparator)
|
||||||
t.Log(sortedPeopleByAge)
|
t.Log(sortedPeopleByAge)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user