Skip to content
On this page

# 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/search.go
- https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.go

## Usage ​

go

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

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

## Index ​

- BubbleSort
- InsertionSort
- SelectionSort
- ShellSort
- QuickSort
- HeapSort
- MergeSort
- CountSort
- BinarySearch
- BinaryIterativeSearch
- LinearSearch
- LRUCache

## Documentation ​

### BubbleSort ​

Sort slice with bubble sort algorithm. Param comparator should implements lancetconstraints.Comparator.

Signature:

go

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

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

Example: Run

go

    package main

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

    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
    }

    func main() {
        numbers := []int{2, 1, 5, 3, 6, 4}
        comparator := &intComparator{}

        algorithm.BubbleSort(numbers, comparator)

        fmt.Println(numbers)

        // Output:
        // [1 2 3 4 5 6]
    }

    package main

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

    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
    }

    func main() {
        numbers := []int{2, 1, 5, 3, 6, 4}
        comparator := &intComparator{}

        algorithm.BubbleSort(numbers, comparator)

        fmt.Println(numbers)

        // Output:
        // [1 2 3 4 5 6]
    }

### InsertionSort ​

Sort slice with insertion sort algorithm. Param comparator should implements lancetconstraints.Comparator.

Signature:

go

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

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

Example: Run

go

    package main

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

    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
    }

    func main() {
        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(peoples)

        // Output:
        // [{d 8} {b 10} {c 17} {a 20} {e 28}]
    }

    package main

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

    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
    }

    func main() {
        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(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:

go

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

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

Example: Run

go

    package main

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

    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
    }

    func main() {
        numbers := []int{2, 1, 5, 3, 6, 4}
        comparator := &intComparator{}

        algorithm.SelectionSort(numbers, comparator)

        fmt.Println(numbers)

        // Output:
        // [1 2 3 4 5 6]
    }

    package main

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

    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
    }

    func main() {
        numbers := []int{2, 1, 5, 3, 6, 4}
        comparator := &intComparator{}

        algorithm.SelectionSort(numbers, comparator)

        fmt.Println(numbers)

        // Output:
        // [1 2 3 4 5 6]
    }

### ShellSort ​

Sort slice with shell sort algorithm. Param comparator should implements lancetconstraints.Comparator.

Signature:

go

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

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

Example: Run

go

    package main

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

    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
    }

    func main() {
        numbers := []int{2, 1, 5, 3, 6, 4}
        comparator := &intComparator{}

        algorithm.ShellSort(numbers, comparator)

        fmt.Println(numbers)

        // Output:
        // [1 2 3 4 5 6]
    }

    package main

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

    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
    }

    func main() {
        numbers := []int{2, 1, 5, 3, 6, 4}
        comparator := &intComparator{}

        algorithm.ShellSort(numbers, comparator)

        fmt.Println(numbers)

        // Output:
        // [1 2 3 4 5 6]
    }

### QuickSort ​

Sort slice with quick sort algorithm. Param comparator should implements lancetconstraints.Comparator.

Signature:

go

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

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

Example:Run

go

    package main

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

    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
    }

    func main() {
        numbers := []int{2, 1, 5, 3, 6, 4}
        comparator := &intComparator{}

        algorithm.QuickSort(numbers, comparator)

        fmt.Println(numbers)

        // Output:
        // [1 2 3 4 5 6]
    }

    package main

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

    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
    }

    func main() {
        numbers := []int{2, 1, 5, 3, 6, 4}
        comparator := &intComparator{}

        algorithm.QuickSort(numbers, comparator)

        fmt.Println(numbers)

        // Output:
        // [1 2 3 4 5 6]
    }

### HeapSort ​

Sort slice with heap sort algorithm. Param comparator should implements lancetconstraints.Comparator.

Signature:

go

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

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

Example: Run

go

    package main

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

    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
    }

    func main() {
        numbers := []int{2, 1, 5, 3, 6, 4}
        comparator := &intComparator{}

        algorithm.HeapSort(numbers, comparator)

        fmt.Println(numbers)

        // Output:
        // [1 2 3 4 5 6]
    }

    package main

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

    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
    }

    func main() {
        numbers := []int{2, 1, 5, 3, 6, 4}
        comparator := &intComparator{}

        algorithm.HeapSort(numbers, comparator)

        fmt.Println(numbers)

        // Output:
        // [1 2 3 4 5 6]
    }

### MergeSort ​

Sort slice with merge sort algorithm. Param comparator should implements lancetconstraints.Comparator.

Signature:

go

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

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

Example: Run

go

    package main

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

    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
    }

    func main() {
        numbers := []int{2, 1, 5, 3, 6, 4}
        comparator := &intComparator{}

        algorithm.MergeSort(numbers, comparator)

        fmt.Println(numbers)

        // Output:
        // [1 2 3 4 5 6]
    }

    package main

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

    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
    }

    func main() {
        numbers := []int{2, 1, 5, 3, 6, 4}
        comparator := &intComparator{}

        algorithm.MergeSort(numbers, comparator)

        fmt.Println(numbers)

        // Output:
        // [1 2 3 4 5 6]
    }

### CountSort ​

Sort slice with count sort algorithm. Param comparator should implements lancetconstraints.Comparator.

Signature:

go

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

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

Example: Run

go

    package main

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

    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
    }

    func main() {
        numbers := []int{2, 1, 5, 3, 6, 4}
        comparator := &intComparator{}

        sortedNums := algorithm.CountSort(numbers, comparator)

        fmt.Println(sortedNums)

        // Output:
        // [1 2 3 4 5 6]
    }

    package main

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

    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
    }

    func main() {
        numbers := []int{2, 1, 5, 3, 6, 4}
        comparator := &intComparator{}

        sortedNums := algorithm.CountSort(numbers, comparator)

        fmt.Println(sortedNums)

        // Output:
        // [1 2 3 4 5 6]
    }

### 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:

go

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

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

Example: Run

go

    package main

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

    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
    }

    func main() {
        numbers := []int{1, 2, 3, 4, 5, 6, 7, 8}
        comparator := &intComparator{}

        result1 := algorithm.BinarySearch(numbers, 5, 0, len(numbers)-1, comparator)
        result2 := algorithm.BinarySearch(numbers, 9, 0, len(numbers)-1, comparator)

        fmt.Println(result1)
        fmt.Println(result2)

        // Output:
        // 4
        // -1
    }

    package main

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

    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
    }

    func main() {
        numbers := []int{1, 2, 3, 4, 5, 6, 7, 8}
        comparator := &intComparator{}

        result1 := algorithm.BinarySearch(numbers, 5, 0, len(numbers)-1, comparator)
        result2 := algorithm.BinarySearch(numbers, 9, 0, len(numbers)-1, comparator)

        fmt.Println(result1)
        fmt.Println(result2)

        // Output:
        // 4
        // -1
    }

### 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:

go

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

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

Example: Run

go

    package main

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

    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
    }

    func main() {
        numbers := []int{1, 2, 3, 4, 5, 6, 7, 8}
        comparator := &intComparator{}

        result1 := algorithm.BinaryIterativeSearch(numbers, 5, 0, len(numbers)-1, comparator)
        result2 := algorithm.BinaryIterativeSearch(numbers, 9, 0, len(numbers)-1, comparator)

        fmt.Println(result1)
        fmt.Println(result2)

        // Output:
        // 4
        // -1
    }

    package main

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

    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
    }

    func main() {
        numbers := []int{1, 2, 3, 4, 5, 6, 7, 8}
        comparator := &intComparator{}

        result1 := algorithm.BinaryIterativeSearch(numbers, 5, 0, len(numbers)-1, comparator)
        result2 := algorithm.BinaryIterativeSearch(numbers, 9, 0, len(numbers)-1, comparator)

        fmt.Println(result1)
        fmt.Println(result2)

        // Output:
        // 4
        // -1
    }

### 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:

go

    func LinearSearch[T any](slice []T, target T, equal func(a, b T) bool) int

    func LinearSearch[T any](slice []T, target T, equal func(a, b T) bool) int

Example: Run

go

    package main

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

    func main() {
        numbers := []int{3, 4, 5, 3, 2, 1}

        equalFunc := func(a, b int) bool {
            return a == b
        }

        result1 := algorithm.LinearSearch(numbers, 3, equalFunc)
        result2 := algorithm.LinearSearch(numbers, 6, equalFunc)

        fmt.Println(result1)
        fmt.Println(result2)

        // Output:
        // 0
        // -1
    }

    package main

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

    func main() {
        numbers := []int{3, 4, 5, 3, 2, 1}

        equalFunc := func(a, b int) bool {
            return a == b
        }

        result1 := algorithm.LinearSearch(numbers, 3, equalFunc)
        result2 := algorithm.LinearSearch(numbers, 6, equalFunc)

        fmt.Println(result1)
        fmt.Println(result2)

        // Output:
        // 0
        // -1
    }

### LRUCache ​

LRUCache implements mem cache with lru.

Signature:

go

    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)
    func (l *LRUCache[K, V]) Delete(key K) bool
    func (l *LRUCache[K, V]) Len() int

    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)
    func (l *LRUCache[K, V]) Delete(key K) bool
    func (l *LRUCache[K, V]) Len() int

Example: Run

go

    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)

        result1, ok1 := cache.Get(1)
        result2, ok2 := cache.Get(2)
        result3, ok3 := cache.Get(3)

        fmt.Println(result1, ok1)
        fmt.Println(result2, ok2)
        fmt.Println(result3, ok3)

        fmt.Println(cache.Len())

        ok := cache.Delete(2)
        fmt.Println(ok)

        // Output:
        // 1 true
        // 2 true
        // 0 false
        // 2
        // true
    }

    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)

        result1, ok1 := cache.Get(1)
        result2, ok2 := cache.Get(2)
        result3, ok3 := cache.Get(3)

        fmt.Println(result1, ok1)
        fmt.Println(result2, ok2)
        fmt.Println(result3, ok3)

        fmt.Println(cache.Len())

        ok := cache.Delete(2)
        fmt.Println(ok)

        // Output:
        // 1 true
        // 2 true
        // 0 false
        // 2
        // true
    }