-
Notifications
You must be signed in to change notification settings - Fork 3
/
sortAlgorithm.go
68 lines (54 loc) · 2.92 KB
/
sortAlgorithm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
Algorithm
algorithm 算法包实现一些基本算法,sort,search,lrucache。
https://www.golancet.cn/api/packages/algorithm.html#Algorithm
*/
package sliceKit
import (
"github.com/duke-git/lancet/v2/algorithm"
"github.com/duke-git/lancet/v2/lancetconstraints"
)
// BubbleSort 冒泡排序,参数comparator需要实现包lancetconstraints.Comparator。
func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator) {
algorithm.BubbleSort(slice, comparator)
}
// InsertionSort 插入排序,参数comparator需要实现包lancetconstraints.Comparator。
func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) {
algorithm.InsertionSort(slice, comparator)
}
// SelectionSort 选择排序,参数comparator需要实现包lancetconstraints.Comparator。
func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) {
algorithm.SelectionSort(slice, comparator)
}
// ShellSort 希尔排序,参数comparator需要实现包lancetconstraints.Comparator。
func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator) {
algorithm.ShellSort(slice, comparator)
}
// QuickSort 快速排序,参数comparator需要实现包lancetconstraints.Comparator。
func QuickSort[T any](slice []T, comparator lancetconstraints.Comparator) {
algorithm.QuickSort(slice, comparator)
}
// HeapSort 堆排序,参数comparator需要实现包lancetconstraints.Comparator。
func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator) {
algorithm.HeapSort(slice, comparator)
}
// MergeSort 归并排序,参数comparator需要实现包lancetconstraints.Comparator。
func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator) {
algorithm.MergeSort(slice, comparator)
}
// CountSort 计数排序,参数comparator需要实现包lancetconstraints.Comparator。
func CountSort[T any](slice []T, comparator lancetconstraints.Comparator) []T {
return algorithm.CountSort(slice, comparator)
}
// BinarySearch (二分法)二分递归查找,返回元素索引,未找到元素返回-1,参数comparator需要实现包lancetconstraints.Comparator。
func BinarySearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int {
return algorithm.BinarySearch(sortedSlice, target, lowIndex, highIndex, comparator)
}
// BinaryIterativeSearch 二分迭代查找,返回元素索引,未找到元素返回-1,参数comparator需要实现包lancetconstraints.Comparator。
func BinaryIterativeSearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int {
return algorithm.BinaryIterativeSearch(sortedSlice, target, lowIndex, highIndex, comparator)
}
// LinearSearch 基于传入的相等函数线性查找元素,返回元素索引,未找到元素返回-1。
func LinearSearch[T any](slice []T, target T, equal func(a, b T) bool) int {
return algorithm.LinearSearch(slice, target, equal)
}