Documentation
¶
Index ¶
- func InsertSorted[T cmp.Ordered](sorted []T, insert T) []T
- func InsertionSort[T cmp.Ordered](items []T) []T
- func MergeSort[T cmp.Ordered](items []T) []T
- func MergeSortedSets[T cmp.Ordered](a, b []T) []T
- func QuickSort[T cmp.Ordered](items []T) []Tdeprecated
- func RadixSort[T cmp.Ordered](items []T) []T
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func InsertSorted ¶
InsertSorted inserts a single element into an already sorted slice. It is more efficient at this operation than resorting the entire array.
func InsertionSort ¶
InsertionSort implements insertion sort for all ordered primitive types. It is only good for very small slices or almost sorted data. For larger, unsorted values its complexity is O(n²), leading to very poor performance.
func MergeSort ¶
MergeSort implements merge sort for all ordered primitive types. It is a stable sorting algorithm, therefore maintaining the order of elements that have the same value. The implementation does not operate in-place, temporarily allocating a copy of the data that needs to be sorted. The worst-case performance is O(n log n) with a static space requirement of O(2n)
func MergeSortedSets ¶
MergeSortedSets merges two already sorted slices very efficiently. It is used as part of merge sort but can also be useful when inserting multiple elements into a sorted slice (though the elements to insert first have to be sorted) or when combining the results of distributed sorting algoritms.
func QuickSort
deprecated
func RadixSort ¶
RadixSort implements radix sort using byte-by-byte sorting with 256 buckets for all integer types. It creates an internal copy of the supplied data, leading to one large allocation. The result is updated in-place and returned for convenience as well. Other data types such as float64, float32 and string are handled via a fallback to slices.Sort. Signed integers are handled by flipping the sign bit before and after sorting and treating them as unsigned integers. The computational complexity is O(n) with a space requirement of O(2n).
Types ¶
This section is empty.