Documentation ¶
Overview ¶
The quickselect package provides primitives for finding the smallest k elements in slices and user-defined collections. The primitives used in the package are modeled off of the standard sort library for Go. Quickselect uses Hoare's Selection Algorithm which finds the smallest k elements in expected O(n) time, and is thus an asymptotically optimal algorithm (and is faster than sorting or heap implementations).
Example ¶
package main import ( "fmt" "github.com/wangjohn/quickselect" ) type Person struct { Name string Age int } func (p Person) String() string { return fmt.Sprintf("%s: %d", p.Name, p.Age) } type ByAge []Person func (a ByAge) Len() int { return len(a) } func (a ByAge) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a ByAge) Less(i, j int) bool { return a[i].Age < a[j].Age } func main() { people := []Person{ {"Bob", 31}, {"John", 42}, {"Michael", 17}, {"Jenny", 26}, } quickselect.QuickSelect(ByAge(people), 2) fmt.Println(people[:2]) }
Output: [Michael: 17 Jenny: 26]
Example (IntQuickSelect) ¶
package main import ( "fmt" "github.com/wangjohn/quickselect" ) func main() { integers := []int{5, 2, 6, 3, 1, 4} quickselect.IntQuickSelect(integers, 3) fmt.Println(integers[:3]) }
Output: [2 3 1]
Example (IntSlice) ¶
package main import ( "fmt" "github.com/wangjohn/quickselect" ) func main() { integers := []int{5, 2, 6, 3, 1, 4} quickselect.QuickSelect(quickselect.IntSlice(integers), 3) fmt.Println(integers[:3]) }
Output: [2 3 1]
Example (ReverseQuickSelect) ¶
package main import ( "fmt" "github.com/wangjohn/quickselect" ) func main() { integers := []int{5, 2, 6, 3, 1, 4} quickselect.QuickSelect(quickselect.Reverse(quickselect.IntSlice(integers)), 3) fmt.Println(integers[:3]) }
Output: [5 6 4]
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Float64QuickSelect ¶
Float64Select mutates the data so that the first k elements in the float64 slice are the k smallest elements in the slice. This is a convenience method for QuickSelect on float64 slices.
func IntQuickSelect ¶
IntQuickSelect mutates the data so that the first k elements in the int slice are the k smallest elements in the slice. This is a convenience method for QuickSelect on int slices.
func QuickSelect ¶
QuickSelect swaps elements in the data provided so that the first k elements (i.e. the elements occuping indices 0, 1, ..., k-1) are the smallest k elements in the data.
QuickSelect implements Hoare's Selection Algorithm and runs in O(n) time, so it is asymptotically faster than sorting or other heap-like implementations for finding the smallest k elements in a data structure.
Note that k must be in the range [0, data.Len()), otherwise the QuickSelect method will raise an error.
func StringQuickSelect ¶
StringQuickSelect mutates the data so that the first k elements in the string slice are the k smallest elements in the slice. This is a convenience method for QuickSelect on string slices.
Types ¶
type Float64Slice ¶
type Float64Slice []float64
The Float64Slice type attaches the QuickSelect interface to an array of float64s. It implements Interface so that you can call QuickSelect(k) on any Float64Slice.
func (Float64Slice) Len ¶
func (t Float64Slice) Len() int
func (Float64Slice) Less ¶
func (t Float64Slice) Less(i, j int) bool
func (Float64Slice) QuickSelect ¶
func (t Float64Slice) QuickSelect(k int) error
QuickSelect(k) mutates the Float64Slice so that the first k elements in the Float64Slice are the k smallest elements in the slice. This is a convenience method for QuickSelect
func (Float64Slice) Swap ¶
func (t Float64Slice) Swap(i, j int)
type IntSlice ¶
type IntSlice []int
The IntSlice type attaches the QuickSelect interface to an array of ints. It implements Interface so that you can call QuickSelect(k) on any IntSlice.
func (IntSlice) QuickSelect ¶
QuickSelect(k) mutates the IntSlice so that the first k elements in the IntSlice are the k smallest elements in the slice. This is a convenience method for QuickSelect
type Interface ¶
type Interface interface { // Len is the number of elements in the collection Len() int // Less reports whether the element with // index i should sort before the element with index j Less(i, j int) bool // Swap swaps the order of elements i and j Swap(i, j int) }
A type, typically a collection, which satisfies quickselect.Interface can be used as data in the QuickSelect method. The interface is the same as the interface required by Go's canonical sorting package (sort.Interface).
Note that the methods require that the elements of the collection be enumerated by an integer index.
type StringSlice ¶
type StringSlice []string
The StringSlice type attaches the QuickSelect interface to an array of float64s. It implements Interface so that you can call QuickSelect(k) on any StringSlice.
func (StringSlice) Len ¶
func (t StringSlice) Len() int
func (StringSlice) Less ¶
func (t StringSlice) Less(i, j int) bool
func (StringSlice) QuickSelect ¶
func (t StringSlice) QuickSelect(k int) error
QuickSelect(k) mutates the StringSlice so that the first k elements in the StringSlice are the k smallest elements in the slice. This is a convenience method for QuickSelect
func (StringSlice) Swap ¶
func (t StringSlice) Swap(i, j int)