Documentation ¶
Overview ¶
Package sortp is a plus to the standard "sort" package.
Index ¶
- func Bubble(data sort.Interface)
- func BubbleF(Len int, Less func(i, j int) bool, Swap func(i, j int))
- func DiffSortedList(LeftLen, RightLen int, Compare func(l, r int) int, ...)
- func IndexSort(data sort.Interface) []int
- func IndexSortF(Len int, Less func(i, j int) bool) []int
- func IndexStable(data sort.Interface) []int
- func IndexStableF(Len int, Less func(i, j int) bool) []int
- func IsSortedF(Len int, Less func(i, j int) bool) bool
- func Merge(LeftLen, RightLen int, Less func(l, r int) bool, AppendLeft func(l int), ...)
- func ReverseLess(Less func(i, j int) bool) func(i, j int) bool
- func SortF(Len int, Less func(i, j int) bool, Swap func(i, j int))
- func StableF(Len int, Less func(i, j int) bool, Swap func(i, j int))
- type InterfaceStruct
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Bubble ¶
The bubble sort algorithm. It is especially useful for almost sorted list. Bubble sort is a stable sort algorithm.
func BubbleF ¶
Similar to Bubble but using closures other than sort.Interface.
Example ¶
data := []int{5, 3, 1, 8, 0} BubbleF(len(data), func(i, j int) bool { return data[i] < data[j] }, func(i, j int) { data[i], data[j] = data[j], data[i] }) fmt.Println(data)
Output: [0 1 3 5 8]
func DiffSortedList ¶
func DiffSortedList(LeftLen, RightLen int, Compare func(l, r int) int, OutputLeft, OutputRight func(index int))
DiffSortedList compares the difference of two sorted list. LeftLen and RightLen are lengths of the left/right lists, respectively. Compare returns -1/1/0 if l-th element of left list is less/greater/equal to the r-th element of the right list, both are zero-based. OutputLeft is called with the index of the element existing in the left list but not in the right list. OutputLeft will be called with ascending indexes. OutputRight is similar. Please make no assumption of the calling order of OutputLeft/OutputRight.
Example ¶
from := []string{"a", "b", "d", "f"} to := []string{"b", "c", "d", "g", "h"} var extra, missing []string DiffSortedList(len(from), len(to), func(f, t int) int { if from[f] < to[t] { return -1 } if from[f] > to[t] { return 1 } return 0 }, func(f int) { extra = append(extra, from[f]) }, func(t int) { missing = append(missing, to[t]) }) fmt.Println("extra:", extra) fmt.Println("missing:", missing) from, to = to, from extra, missing = nil, nil DiffSortedList(len(from), len(to), func(f, t int) int { if from[f] < to[t] { return -1 } if from[f] > to[t] { return 1 } return 0 }, func(f int) { extra = append(extra, from[f]) }, func(t int) { missing = append(missing, to[t]) }) fmt.Println("extra:", extra) fmt.Println("missing:", missing) to = from extra, missing = nil, nil DiffSortedList(len(from), len(to), func(f, t int) int { if from[f] < to[t] { return -1 } if from[f] > to[t] { return 1 } return 0 }, func(f int) { extra = append(extra, from[f]) }, func(t int) { missing = append(missing, to[t]) }) fmt.Println("extra:", extra) fmt.Println("missing:", missing)
Output: extra: [a f] missing: [c g h] extra: [c g h] missing: [a f] extra: [] missing: []
func IndexSort ¶
IndexSort returns a slice of indexes l, so that data[l[i]], i = 0...N-1, are sorted.
func IndexSortF ¶
IndexSortF is similar to IndexSort but with funcs as the input.
func IndexStable ¶
IndexSort returns a slice of indexes l, so that data[l[i]], i = 0...N-1, are sorted.
func IndexStableF ¶
IndexStableF is similar to IndexStable but with funcs as the input.
func Merge ¶
func Merge(LeftLen, RightLen int, Less func(l, r int) bool, AppendLeft func(l int), AppendRight func(r int))
Merge merges two sorted list.
@param LeftLen is the length of the left list. @param RightLen is the length of the right list. @param Less compares the l-th element of left list and the r-th element of the right list. @param AppendLeft appends the l-th element of the left list to the result list. @param AppendRight appends the r-th element of the right list to the result list.
Example ¶
left := []int{1, 3, 5, 7} right := []int{4, 6, 8, 10, 11} var merged []int Merge(len(left), len(right), func(l, r int) bool { return left[l] < right[r] }, func(l int) { merged = append(merged, left[l]) }, func(r int) { merged = append(merged, right[r]) }) fmt.Println(merged) left, right = right, left merged = nil Merge(len(left), len(right), func(l, r int) bool { return left[l] < right[r] }, func(l int) { merged = append(merged, left[l]) }, func(r int) { merged = append(merged, right[r]) }) fmt.Println(merged)
Output: [1 3 4 5 6 7 8 10 11] [1 3 4 5 6 7 8 10 11]
func ReverseLess ¶
ReverseLess returns a func which can be used to sort data in a reverse order.
Types ¶
type InterfaceStruct ¶
type InterfaceStruct struct { // Len is the number of elements in the collection. LenF func() int // Less reports whether the element with // index i should sort before the element with index j. LessF func(i, j int) bool // Swap swaps the elements with indexes i and j. SwapF func(i, j int) }
InterfaceStruct is a struct implementing sort.Interface given closures
Example ¶
data := []int{5, 3, 1, 8, 0} sort.Sort(InterfaceStruct{ LenF: func() int { return len(data) }, LessF: func(i, j int) bool { return data[i] < data[j] }, SwapF: func(i, j int) { data[i], data[j] = data[j], data[i] }, }) fmt.Println(data)
Output: [0 1 3 5 8]