mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
doc: update doc for algorithm package
This commit is contained in:
@@ -60,7 +60,6 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
|
||||||
type intComparator struct{}
|
type intComparator struct{}
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
@@ -76,6 +75,7 @@ func main() {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
algorithm.BubbleSort(intSlice, comparator)
|
algorithm.BubbleSort(intSlice, comparator)
|
||||||
@@ -85,8 +85,6 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="InsertionSort">InsertionSort</span>
|
### <span id="InsertionSort">InsertionSort</span>
|
||||||
<p>Sort slice with insertion sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
<p>Sort slice with insertion sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
||||||
|
|
||||||
@@ -105,7 +103,6 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
|
||||||
type people struct {
|
type people struct {
|
||||||
Name string
|
Name string
|
||||||
Age int
|
Age int
|
||||||
@@ -135,6 +132,7 @@ func main() {
|
|||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
var peoples = []people{
|
var peoples = []people{
|
||||||
{Name: "a", Age: 20},
|
{Name: "a", Age: 20},
|
||||||
{Name: "b", Age: 10},
|
{Name: "b", Age: 10},
|
||||||
@@ -151,7 +149,6 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="SelectionSort">SelectionSort</span>
|
### <span id="SelectionSort">SelectionSort</span>
|
||||||
<p>Sort slice with selection sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
<p>Sort slice with selection sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
||||||
|
|
||||||
@@ -170,7 +167,6 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
|
||||||
type intComparator struct{}
|
type intComparator struct{}
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
@@ -186,6 +182,7 @@ func main() {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
algorithm.SelectionSort(intSlice, comparator)
|
algorithm.SelectionSort(intSlice, comparator)
|
||||||
@@ -195,8 +192,6 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ShellSort">ShellSort</span>
|
### <span id="ShellSort">ShellSort</span>
|
||||||
<p>Sort slice with shell sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
<p>Sort slice with shell sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
||||||
|
|
||||||
@@ -215,7 +210,6 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
|
||||||
type intComparator struct{}
|
type intComparator struct{}
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
@@ -231,6 +225,7 @@ func main() {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
algorithm.ShellSort(intSlice, comparator)
|
algorithm.ShellSort(intSlice, comparator)
|
||||||
@@ -240,8 +235,6 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="QuickSort">QuickSort</span>
|
### <span id="QuickSort">QuickSort</span>
|
||||||
<p>Sort slice with quick sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
<p>Sort slice with quick sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
||||||
|
|
||||||
@@ -260,7 +253,6 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
|
||||||
type intComparator struct{}
|
type intComparator struct{}
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
@@ -276,6 +268,7 @@ func main() {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
algorithm.QuickSort(intSlice, comparator)
|
algorithm.QuickSort(intSlice, comparator)
|
||||||
@@ -285,8 +278,6 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HeapSort">HeapSort</span>
|
### <span id="HeapSort">HeapSort</span>
|
||||||
<p>Sort slice with heap sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
<p>Sort slice with heap sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
||||||
|
|
||||||
@@ -305,7 +296,6 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
|
||||||
type intComparator struct{}
|
type intComparator struct{}
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
@@ -321,6 +311,7 @@ func main() {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
algorithm.HeapSort(intSlice, comparator)
|
algorithm.HeapSort(intSlice, comparator)
|
||||||
@@ -330,8 +321,6 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="MergeSort">MergeSort</span>
|
### <span id="MergeSort">MergeSort</span>
|
||||||
<p>Sort slice with merge sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
<p>Sort slice with merge sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
||||||
|
|
||||||
@@ -350,7 +339,6 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
|
||||||
type intComparator struct{}
|
type intComparator struct{}
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
@@ -366,6 +354,7 @@ func main() {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
algorithm.MergeSort(intSlice, comparator)
|
algorithm.MergeSort(intSlice, comparator)
|
||||||
@@ -375,7 +364,6 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="CountSort">CountSort</span>
|
### <span id="CountSort">CountSort</span>
|
||||||
<p>Sort slice with count sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
<p>Sort slice with count sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
||||||
|
|
||||||
@@ -394,7 +382,7 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
|
||||||
type intComparator struct{}
|
type intComparator struct{}
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
@@ -410,6 +398,7 @@ func main() {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
sortedSlice := algorithm.CountSort(intSlice, comparator)
|
sortedSlice := algorithm.CountSort(intSlice, comparator)
|
||||||
@@ -419,8 +408,6 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="BinarySearch">BinarySearch</span>
|
### <span id="BinarySearch">BinarySearch</span>
|
||||||
<p>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.</p>
|
<p>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.</p>
|
||||||
|
|
||||||
@@ -439,7 +426,6 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
|
||||||
type intComparator struct{}
|
type intComparator struct{}
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
@@ -455,6 +441,7 @@ func main() {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
var sortedNumbers = []int{1, 2, 3, 4, 5, 6, 7, 8}
|
var sortedNumbers = []int{1, 2, 3, 4, 5, 6, 7, 8}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
foundIndex := algorithm.BinarySearch(sortedNumbers, 5, 0, len(sortedNumbers)-1, comparator)
|
foundIndex := algorithm.BinarySearch(sortedNumbers, 5, 0, len(sortedNumbers)-1, comparator)
|
||||||
@@ -465,8 +452,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="BinaryIterativeSearch">BinaryIterativeSearch</span>
|
### <span id="BinaryIterativeSearch">BinaryIterativeSearch</span>
|
||||||
<p>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.</p>
|
<p>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.</p>
|
||||||
|
|
||||||
@@ -485,7 +470,6 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
|
||||||
type intComparator struct{}
|
type intComparator struct{}
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
@@ -501,6 +485,7 @@ func main() {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
var sortedNumbers = []int{1, 2, 3, 4, 5, 6, 7, 8}
|
var sortedNumbers = []int{1, 2, 3, 4, 5, 6, 7, 8}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
foundIndex := algorithm.BinaryIterativeSearch(sortedNumbers, 5, 0, len(sortedNumbers)-1, comparator)
|
foundIndex := algorithm.BinaryIterativeSearch(sortedNumbers, 5, 0, len(sortedNumbers)-1, comparator)
|
||||||
@@ -512,15 +497,13 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="LinearSearch">LinearSearch</span>
|
### <span id="LinearSearch">LinearSearch</span>
|
||||||
<p>LinearSearch Simple linear search algorithm that iterates over all elements of an slice. If a target is found, the index of the target is returned. Else the function return -1.</p>
|
<p>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.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func LinearSearch[T any](slice []T, target T, comparator lancetconstraints.Comparator) int
|
func LinearSearch[T any](slice []T, target T, equal func(a, b T) bool) int
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -533,34 +516,25 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
type intComparator struct{}
|
numbers := []int{3, 4, 5, 3, 2, 1}
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
equalFunc := func(a, b int) bool {
|
||||||
val1, _ := v1.(int)
|
return a == b
|
||||||
val2, _ := v2.(int)
|
|
||||||
|
|
||||||
//ascending order
|
|
||||||
if val1 < val2 {
|
|
||||||
return -1
|
|
||||||
} else if val1 > val2 {
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
result1 := algorithm.LinearSearch(numbers, 3, equalFunc)
|
||||||
comparator := &intComparator{}
|
result2 := algorithm.LinearSearch(numbers, 6, equalFunc)
|
||||||
foundIndex := algorithm.LinearSearch(intSlice, 5, comparator)
|
|
||||||
fmt.Println(foundIndex) //2
|
|
||||||
|
|
||||||
notFoundIndex := algorithm.LinearSearch(sortedNumbers, 0, comparator)
|
fmt.Println(result1)
|
||||||
fmt.Println(notFoundIndex) //-1
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 0
|
||||||
|
// -1
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="LRUCache">LRUCache</span>
|
### <span id="LRUCache">LRUCache</span>
|
||||||
<p>LRUCache implements mem cache with lru.</p>
|
<p>LRUCache implements mem cache with lru.</p>
|
||||||
|
|
||||||
|
|||||||
@@ -40,8 +40,6 @@ import (
|
|||||||
|
|
||||||
## 文档
|
## 文档
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="BubbleSort">BubbleSort</span>
|
### <span id="BubbleSort">BubbleSort</span>
|
||||||
<p>冒泡排序,参数comparator需要实现包lancetconstraints.Comparator</p>
|
<p>冒泡排序,参数comparator需要实现包lancetconstraints.Comparator</p>
|
||||||
|
|
||||||
@@ -60,7 +58,6 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
|
||||||
type intComparator struct{}
|
type intComparator struct{}
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
@@ -76,17 +73,16 @@ func main() {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
algorithm.BubbleSort(intSlice, comparator)
|
algorithm.BubbleSort(intSlice, comparator)
|
||||||
|
|
||||||
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="InsertionSort">InsertionSort</span>
|
### <span id="InsertionSort">InsertionSort</span>
|
||||||
<p>插入排序,参数comparator需要实现包lancetconstraints.Comparator</p>
|
<p>插入排序,参数comparator需要实现包lancetconstraints.Comparator</p>
|
||||||
|
|
||||||
@@ -105,7 +101,6 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
|
||||||
type people struct {
|
type people struct {
|
||||||
Name string
|
Name string
|
||||||
Age int
|
Age int
|
||||||
@@ -135,6 +130,7 @@ func main() {
|
|||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
var peoples = []people{
|
var peoples = []people{
|
||||||
{Name: "a", Age: 20},
|
{Name: "a", Age: 20},
|
||||||
{Name: "b", Age: 10},
|
{Name: "b", Age: 10},
|
||||||
@@ -145,13 +141,10 @@ func main() {
|
|||||||
comparator := &peopleAgeComparator{}
|
comparator := &peopleAgeComparator{}
|
||||||
algorithm.InsertionSort(peoples, comparator)
|
algorithm.InsertionSort(peoples, comparator)
|
||||||
|
|
||||||
fmt.Println(intSlice) //[{d 8} {b 10} {c 17} {a 20} {e 28}]
|
fmt.Println(peoples) //[{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>
|
||||||
|
|
||||||
@@ -170,7 +163,6 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
|
||||||
type intComparator struct{}
|
type intComparator struct{}
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
@@ -186,6 +178,7 @@ func main() {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
algorithm.SelectionSort(intSlice, comparator)
|
algorithm.SelectionSort(intSlice, comparator)
|
||||||
@@ -194,9 +187,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ShellSort">ShellSort</span>
|
### <span id="ShellSort">ShellSort</span>
|
||||||
<p>希尔排序,参数comparator需要实现包lancetconstraints.Comparator</p>
|
<p>希尔排序,参数comparator需要实现包lancetconstraints.Comparator</p>
|
||||||
|
|
||||||
@@ -215,7 +205,6 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
|
||||||
type intComparator struct{}
|
type intComparator struct{}
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
@@ -231,6 +220,7 @@ func main() {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
algorithm.ShellSort(intSlice, comparator)
|
algorithm.ShellSort(intSlice, comparator)
|
||||||
@@ -240,8 +230,6 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="QuickSort">QuickSort</span>
|
### <span id="QuickSort">QuickSort</span>
|
||||||
<p>快速排序,参数comparator需要实现包lancetconstraints.Comparator</p>
|
<p>快速排序,参数comparator需要实现包lancetconstraints.Comparator</p>
|
||||||
|
|
||||||
@@ -260,7 +248,6 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
|
||||||
type intComparator struct{}
|
type intComparator struct{}
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
@@ -276,6 +263,7 @@ func main() {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
algorithm.QuickSort(intSlice, comparator)
|
algorithm.QuickSort(intSlice, comparator)
|
||||||
@@ -285,8 +273,6 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HeapSort">HeapSort</span>
|
### <span id="HeapSort">HeapSort</span>
|
||||||
<p>堆排序,参数comparator需要实现包lancetconstraints.Comparator</p>
|
<p>堆排序,参数comparator需要实现包lancetconstraints.Comparator</p>
|
||||||
|
|
||||||
@@ -305,7 +291,6 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
|
||||||
type intComparator struct{}
|
type intComparator struct{}
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
@@ -321,17 +306,16 @@ func main() {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
algorithm.HeapSort(intSlice, comparator)
|
algorithm.HeapSort(intSlice, comparator)
|
||||||
|
|
||||||
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="MergeSort">MergeSort</span>
|
### <span id="MergeSort">MergeSort</span>
|
||||||
<p>归并排序,参数comparator需要实现包lancetconstraints.Comparator</p>
|
<p>归并排序,参数comparator需要实现包lancetconstraints.Comparator</p>
|
||||||
|
|
||||||
@@ -350,7 +334,6 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
|
||||||
type intComparator struct{}
|
type intComparator struct{}
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
@@ -366,6 +349,7 @@ func main() {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
algorithm.MergeSort(intSlice, comparator)
|
algorithm.MergeSort(intSlice, comparator)
|
||||||
@@ -375,7 +359,6 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="CountSort">CountSort</span>
|
### <span id="CountSort">CountSort</span>
|
||||||
<p>计数排序,参数comparator需要实现包lancetconstraints.Comparator</p>
|
<p>计数排序,参数comparator需要实现包lancetconstraints.Comparator</p>
|
||||||
|
|
||||||
@@ -394,7 +377,6 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
|
||||||
type intComparator struct{}
|
type intComparator struct{}
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
@@ -410,6 +392,7 @@ func main() {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
sortedSlice := algorithm.CountSort(intSlice, comparator)
|
sortedSlice := algorithm.CountSort(intSlice, comparator)
|
||||||
@@ -419,8 +402,6 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="BinarySearch">BinarySearch</span>
|
### <span id="BinarySearch">BinarySearch</span>
|
||||||
<p>二分递归查找,返回元素索引,未找到元素返回-1,参数comparator需要实现包lancetconstraints.Comparator</p>
|
<p>二分递归查找,返回元素索引,未找到元素返回-1,参数comparator需要实现包lancetconstraints.Comparator</p>
|
||||||
|
|
||||||
@@ -439,7 +420,6 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
|
||||||
type intComparator struct{}
|
type intComparator struct{}
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
@@ -455,6 +435,7 @@ func main() {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
var sortedNumbers = []int{1, 2, 3, 4, 5, 6, 7, 8}
|
var sortedNumbers = []int{1, 2, 3, 4, 5, 6, 7, 8}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
foundIndex := algorithm.BinarySearch(sortedNumbers, 5, 0, len(sortedNumbers)-1, comparator)
|
foundIndex := algorithm.BinarySearch(sortedNumbers, 5, 0, len(sortedNumbers)-1, comparator)
|
||||||
@@ -463,8 +444,8 @@ func main() {
|
|||||||
notFoundIndex := algorithm.BinarySearch(sortedNumbers, 9, 0, len(sortedNumbers)-1, comparator)
|
notFoundIndex := algorithm.BinarySearch(sortedNumbers, 9, 0, len(sortedNumbers)-1, comparator)
|
||||||
fmt.Println(notFoundIndex) //-1
|
fmt.Println(notFoundIndex) //-1
|
||||||
}
|
}
|
||||||
```
|
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="BinaryIterativeSearch">BinaryIterativeSearch</span>
|
### <span id="BinaryIterativeSearch">BinaryIterativeSearch</span>
|
||||||
@@ -485,7 +466,6 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
|
||||||
type intComparator struct{}
|
type intComparator struct{}
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
@@ -501,6 +481,7 @@ func main() {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
var sortedNumbers = []int{1, 2, 3, 4, 5, 6, 7, 8}
|
var sortedNumbers = []int{1, 2, 3, 4, 5, 6, 7, 8}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
foundIndex := algorithm.BinaryIterativeSearch(sortedNumbers, 5, 0, len(sortedNumbers)-1, comparator)
|
foundIndex := algorithm.BinaryIterativeSearch(sortedNumbers, 5, 0, len(sortedNumbers)-1, comparator)
|
||||||
@@ -515,12 +496,12 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
### <span id="LinearSearch">LinearSearch</span>
|
### <span id="LinearSearch">LinearSearch</span>
|
||||||
<p>线性查找,返回元素索引,未找到元素返回-1,参数comparator需要实现包lancetconstraints.Comparator</p>
|
<p>基于传入的相等函数线性查找元素,返回元素索引,未找到元素返回-1</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func LinearSearch[T any](slice []T, target T, comparator lancetconstraints.Comparator) int
|
func LinearSearch[T any](slice []T, target T, equal func(a, b T) bool) int
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -533,34 +514,25 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
type intComparator struct{}
|
numbers := []int{3, 4, 5, 3, 2, 1}
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
equalFunc := func(a, b int) bool {
|
||||||
val1, _ := v1.(int)
|
return a == b
|
||||||
val2, _ := v2.(int)
|
|
||||||
|
|
||||||
//ascending order
|
|
||||||
if val1 < val2 {
|
|
||||||
return -1
|
|
||||||
} else if val1 > val2 {
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
result1 := algorithm.LinearSearch(numbers, 3, equalFunc)
|
||||||
comparator := &intComparator{}
|
result2 := algorithm.LinearSearch(numbers, 6, equalFunc)
|
||||||
foundIndex := algorithm.LinearSearch(intSlice, 5, comparator)
|
|
||||||
fmt.Println(foundIndex) //2
|
|
||||||
|
|
||||||
notFoundIndex := algorithm.LinearSearch(sortedNumbers, 0, comparator)
|
fmt.Println(result1)
|
||||||
fmt.Println(notFoundIndex) //-1
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 0
|
||||||
|
// -1
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="LRUCache">LRUCache</span>
|
### <span id="LRUCache">LRUCache</span>
|
||||||
<p>lru实现缓存</p>
|
<p>lru实现缓存</p>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user