algorithm

package
v0.3.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 16, 2023 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AscSort

func AscSort[T any](params []T)

AscSort 切片正序排序

func BubbleSort

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

BubbleSort applys the bubble sort algorithm to sort the collection, will change the original collection data. Play: https://go.dev/play/p/GNdv7Jg2Taj

Example
numbers := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{}

BubbleSort(numbers, comparator)

fmt.Println(numbers)
Output:

[1 2 3 4 5 6]

func CountSort

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

CountSort applys the count sort algorithm to sort the collection, don't change the original collection data. Play: https://go.dev/play/p/tB-Umgm0DrP

Example
numbers := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{}

sortedNumber := CountSort(numbers, comparator)

fmt.Println(numbers)
fmt.Println(sortedNumber)
Output:

[2 1 5 3 6 4]
[1 2 3 4 5 6]

func DescSort

func DescSort[T any](params []T)

DescSort 切片倒叙排序

func HeapSort

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

HeapSort applys the heap sort algorithm to sort the collection, will change the original collection data. Play: https://go.dev/play/p/u6Iwa1VZS_f

Example
numbers := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{}

HeapSort(numbers, comparator)

fmt.Println(numbers)
Output:

[1 2 3 4 5 6]

func InsertionSort

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

InsertionSort applys the insertion sort algorithm to sort the collection, will change the original collection data. Play: https://go.dev/play/p/G5LJiWgJJW6

Example
numbers := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{}

InsertionSort(numbers, comparator)

fmt.Println(numbers)
Output:

[1 2 3 4 5 6]

func MergeSort

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

MergeSort applys the merge sort algorithm to sort the collection, will change the original collection data. Play: https://go.dev/play/p/ydinn9YzUJn

Example
numbers := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{}

MergeSort(numbers, comparator)

fmt.Println(numbers)
Output:

[1 2 3 4 5 6]

func QuickSort

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

QuickSort quick sorting for slice, lowIndex is 0 and highIndex is len(slice)-1. Play: https://go.dev/play/p/7Y7c1Elk3ax

Example
numbers := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{}

QuickSort(numbers, comparator)

fmt.Println(numbers)
Output:

[1 2 3 4 5 6]

func SelectionSort

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

SelectionSort applys the selection sort algorithm to sort the collection, will change the original collection data. Play: https://go.dev/play/p/oXovbkekayS

Example
numbers := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{}

SelectionSort(numbers, comparator)

fmt.Println(numbers)
Output:

[1 2 3 4 5 6]

func ShellSort

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

ShellSort applys the shell sort algorithm to sort the collection, will change the original collection data. Play: https://go.dev/play/p/3ibkszpJEu3

Example
numbers := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{}

ShellSort(numbers, comparator)

fmt.Println(numbers)
Output:

[1 2 3 4 5 6]

func Sort

func Sort[T any](params []T, comparator lancetconstraints.Comparator)

Sort 自定义排序

Types

type AscComparator

type AscComparator struct{}

func (*AscComparator) Compare

func (c *AscComparator) Compare(v1 any, v2 any) int

type DescComparator

type DescComparator struct{}

func (*DescComparator) Compare

func (c *DescComparator) Compare(v1 any, v2 any) int

type LRUCache

type LRUCache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

LRUCache lru cache (thread unsafe)

func NewLRUCache

func NewLRUCache[K comparable, V any](capacity int) *LRUCache[K, V]

NewLRUCache creates a LRUCache pointer instance.

func (*LRUCache[K, V]) Delete

func (l *LRUCache[K, V]) Delete(key K) bool

Delete item from lru cache.

Example
cache := NewLRUCache[int, int](2)

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

result1, ok1 := cache.Get(1)

ok2 := cache.Delete(2)

_, ok3 := cache.Get(2)

fmt.Println(result1, ok1)
fmt.Println(ok2)
fmt.Println(ok3)
Output:

1 true
true
false

func (*LRUCache[K, V]) Get

func (l *LRUCache[K, V]) Get(key K) (V, bool)

Get value of key from lru cache. Play: https://go.dev/play/p/iUynEfOP8G0

Example
cache := 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)
Output:

1 true
2 true
0 false

func (*LRUCache[K, V]) Len

func (l *LRUCache[K, V]) Len() int

Len returns the number of items in the cache.

Example
cache := NewLRUCache[int, int](2)

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

result := cache.Len()

fmt.Println(result)
Output:

2

func (*LRUCache[K, V]) Put

func (l *LRUCache[K, V]) Put(key K, value V)

Put value of key into lru cache. Play: https://go.dev/play/p/iUynEfOP8G0

Example
cache := 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)
Output:

1 true
2 true
0 false

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL