# Algorithm

algorithm算法包实现一些基本算法,sort,search,lrucache。

# 源码

# 用法

import (
    "github.com/duke-git/lancet/v2/algorithm"
)

# 文档

# BubbleSort

冒泡排序,参数comparator需要实现包lancetconstraints.Comparator

函数签名:

func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator)

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/algorithm"
)

func main() {
    type intComparator struct{}

    func (c *intComparator) Compare(v1 any, v2 any) int {
        val1, _ := v1.(int)
        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}
    comparator := &intComparator{}
    algorithm.BubbleSort(intSlice, comparator)

    fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
}

# InsertionSort

插入排序,参数comparator需要实现包lancetconstraints.Comparator

函数签名:

func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator)

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/algorithm"
)

func main() {
    type people struct {
        Name string
        Age  int
    }

    // PeopleAageComparator sort people slice by age field
    type peopleAgeComparator struct{}

    // Compare implements github.com/duke-git/lancet/lancetconstraints/constraints.go/Comparator
    func (pc *peopleAgeComparator) Compare(v1 any, v2 any) int {
        p1, _ := v1.(people)
        p2, _ := v2.(people)

        //ascending order
        if p1.Age < p2.Age {
            return -1
        } else if p1.Age > p2.Age {
            return 1
        }
        return 0

        //decending order
        // if p1.Age > p2.Age {
        // 	return -1
        // } else if p1.Age < p2.Age {
        // 	return 1
        // }
    }

    var peoples = []people{
        {Name: "a", Age: 20},
        {Name: "b", Age: 10},
        {Name: "c", Age: 17},
        {Name: "d", Age: 8},
        {Name: "e", Age: 28},
    }
    comparator := &peopleAgeComparator{}
    algorithm.InsertionSort(peoples, comparator)

    fmt.Println(intSlice) //[{d 8} {b 10} {c 17} {a 20} {e 28}]
}

# SelectionSort

选择排序,参数comparator需要实现包lancetconstraints.Comparator

函数签名:

func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator)

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/algorithm"
)

func main() {
    type intComparator struct{}

    func (c *intComparator) Compare(v1 any, v2 any) int {
        val1, _ := v1.(int)
        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}
    comparator := &intComparator{}
    algorithm.SelectionSort(intSlice, comparator)

    fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
}

# ShellSort

希尔排序,参数comparator需要实现包lancetconstraints.Comparator

函数签名:

func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator)

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/algorithm"
)

func main() {
    type intComparator struct{}

    func (c *intComparator) Compare(v1 any, v2 any) int {
        val1, _ := v1.(int)
        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}
    comparator := &intComparator{}
    algorithm.ShellSort(intSlice, comparator)

    fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
}

# QuickSort

快速排序,参数comparator需要实现包lancetconstraints.Comparator

函数签名:

func QuickSort[T any](slice []T, comparator lancetconstraints.Comparator) []T

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/algorithm"
)

func main() {
    type intComparator struct{}

    func (c *intComparator) Compare(v1 any, v2 any) int {
        val1, _ := v1.(int)
        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}
    comparator := &intComparator{}
    algorithm.QuickSort(intSlice, comparator)

    fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
}

# HeapSort

堆排序,参数comparator需要实现包lancetconstraints.Comparator

函数签名:

func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator) []T

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/algorithm"
)

func main() {
    type intComparator struct{}

    func (c *intComparator) Compare(v1 any, v2 any) int {
        val1, _ := v1.(int)
        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}
    comparator := &intComparator{}
    algorithm.HeapSort(intSlice, comparator)

    fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
}

# MergeSort

归并排序,参数comparator需要实现包lancetconstraints.Comparator

函数签名:

func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator)

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/algorithm"
)

func main() {
    type intComparator struct{}

    func (c *intComparator) Compare(v1 any, v2 any) int {
        val1, _ := v1.(int)
        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}
    comparator := &intComparator{}
    algorithm.MergeSort(intSlice, comparator)

    fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
}

# CountSort

计数排序,参数comparator需要实现包lancetconstraints.Comparator

函数签名:

func CountSort[T any](slice []T, comparator lancetconstraints.Comparator) []T

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/algorithm"
)

func main() {
    type intComparator struct{}

    func (c *intComparator) Compare(v1 any, v2 any) int {
        val1, _ := v1.(int)
        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}
    comparator := &intComparator{}
    sortedSlice := algorithm.CountSort(intSlice, comparator)

    fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
}

# BinarySearch

二分递归查找,返回元素索引,未找到元素返回-1,参数comparator需要实现包lancetconstraints.Comparator

函数签名:

func BinarySearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/algorithm"
)

func main() {
    type intComparator struct{}

    func (c *intComparator) Compare(v1 any, v2 any) int {
        val1, _ := v1.(int)
        val2, _ := v2.(int)

        //ascending order
        if val1 < val2 {
            return -1
        } else if val1 > val2 {
            return 1
        }
        return 0
    }

    var sortedNumbers = []int{1, 2, 3, 4, 5, 6, 7, 8}
    comparator := &intComparator{}
    foundIndex := algorithm.BinarySearch(sortedNumbers, 5, 0, len(sortedNumbers)-1, comparator)
    fmt.Println(foundIndex) //4

    notFoundIndex := algorithm.BinarySearch(sortedNumbers, 9, 0, len(sortedNumbers)-1, comparator)
    fmt.Println(notFoundIndex) //-1
}

# BinaryIterativeSearch

二分迭代查找,返回元素索引,未找到元素返回-1,参数comparator需要实现包lancetconstraints.Comparator

函数签名:

func BinaryIterativeSearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/algorithm"
)

func main() {
    type intComparator struct{}

    func (c *intComparator) Compare(v1 any, v2 any) int {
        val1, _ := v1.(int)
        val2, _ := v2.(int)

        //ascending order
        if val1 < val2 {
            return -1
        } else if val1 > val2 {
            return 1
        }
        return 0
    }

    var sortedNumbers = []int{1, 2, 3, 4, 5, 6, 7, 8}
    comparator := &intComparator{}
    foundIndex := algorithm.BinaryIterativeSearch(sortedNumbers, 5, 0, len(sortedNumbers)-1, comparator)
    fmt.Println(foundIndex) //4

    notFoundIndex := algorithm.BinaryIterativeSearch(sortedNumbers, 9, 0, len(sortedNumbers)-1, comparator)
    fmt.Println(notFoundIndex) //-1
}

# LinearSearch

线性查找,返回元素索引,未找到元素返回-1,参数comparator需要实现包lancetconstraints.Comparator

函数签名:

func LinearSearch[T any](slice []T, target T, comparator lancetconstraints.Comparator) int

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/algorithm"
)

func main() {
    type intComparator struct{}

    func (c *intComparator) Compare(v1 any, v2 any) int {
        val1, _ := v1.(int)
        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}
    comparator := &intComparator{}
    foundIndex := algorithm.LinearSearch(intSlice, 5, comparator)
    fmt.Println(foundIndex) //2

    notFoundIndex := algorithm.LinearSearch(sortedNumbers, 0, comparator)
    fmt.Println(notFoundIndex) //-1
}

# LRUCache

lru实现缓存

函数签名:

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]) Put(key K, value V)

Example:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/algorithm"
)

func main() {
    cache := algorithm.NewLRUCache[int, int](2)

    cache.Put(1, 1)
    cache.Put(2, 2)

    _, ok := cache.Get(0) // ok -> false

    v, ok := cache.Get(1) // v->1, ok->true

}
最后更新时间: 2022/9/29 上午11:23:10