1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-09 23:22:28 +08:00

feat: add SortBy function for slice

This commit is contained in:
dudaodong
2022-12-02 14:53:57 +08:00
parent ec27ad4c40
commit 280ecb5cef
3 changed files with 65 additions and 3 deletions

View File

@@ -29,14 +29,14 @@ func sliceElemType(reflectType reflect.Type) reflect.Type {
func quickSort[T lancetconstraints.Ordered](slice []T, lowIndex, highIndex int, order string) {
if lowIndex < highIndex {
p := partition(slice, lowIndex, highIndex, order)
p := partitionOrderedSlice(slice, lowIndex, highIndex, order)
quickSort(slice, lowIndex, p-1, order)
quickSort(slice, p+1, highIndex, order)
}
}
// partition split slice into two parts for quick sort
func partition[T lancetconstraints.Ordered](slice []T, lowIndex, highIndex int, order string) int {
// partitionOrderedSlice split ordered slice into two parts for quick sort
func partitionOrderedSlice[T lancetconstraints.Ordered](slice []T, lowIndex, highIndex int, order string) int {
p := slice[highIndex]
i := lowIndex
@@ -59,6 +59,32 @@ func partition[T lancetconstraints.Ordered](slice []T, lowIndex, highIndex int,
return i
}
func quickSortBy[T any](slice []T, lowIndex, highIndex int, less func(a, b T) bool) {
if lowIndex < highIndex {
p := partitionAnySlice(slice, lowIndex, highIndex, less)
quickSortBy(slice, lowIndex, p-1, less)
quickSortBy(slice, p+1, highIndex, less)
}
}
// partitionAnySlice split any slice into two parts for quick sort
func partitionAnySlice[T any](slice []T, lowIndex, highIndex int, less func(a, b T) bool) int {
p := slice[highIndex]
i := lowIndex
for j := lowIndex; j < highIndex; j++ {
if less(slice[j], p) {
swap(slice, i, j)
i++
}
}
swap(slice, i, highIndex)
return i
}
// swap two slice value at index i and j
func swap[T any](slice []T, i, j int) {
slice[i], slice[j] = slice[j], slice[i]